C# 基础(十七)C# try-throw-cacth

1、C#控制台项目请自己建立.

2、你可以直接拷贝下面程序到你的控制台,以便测试

3、try-throw-cacth 往往是配对使用。catch 用来捕获throw的异常。

try
{
    if (throwEx)
    {
         throw new Exception("Sorry!  Database error! Tx failed...");
    } 
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);   
}

当throwEx为true,控制台会输出catch捕获的的ex.Message:

猜你喜欢

转载自blog.csdn.net/xpj8888/article/details/86161684