以下是一个简单的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# 抛出异常代码示例:自定义异常处理

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

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