以下是一个简单的C#代码片段,抛出一个自定义异常:

try {
    // some code here
    if (someCondition) {
        throw new Exception("Something went wrong.");
    }
    // more code here
}
catch (Exception ex) {
    Console.WriteLine(ex.Message);
}

在这个例子中,如果“someCondition”为真,就会抛出一个新的异常,然后在catch块中打印出异常的消息。

用C#语言写一个抛出异常的代码片段

原文地址: http://www.cveoy.top/t/topic/brNv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录