csharp C#使用TRY捕获异常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp C#使用TRY捕获异常相关的知识,希望对你有一定的参考价值。

 “dynamic error handling” in that our program is
responding to an error when it occurs. To do this we have to use a new C#
construction, the try – catch clause. The try keyword is followed by a block of code.
After the block of code comes the catch clause. If any of the statements following the
try throws an exception the program runs the code in the catch clause to handle this
error.

int age;
try
{
 age = int.Parse(ageString);
 Console.WriteLine("Thank you");
}
catch
{
 Console.WriteLine("Invalid age value");
}


The code above uses Parse to decode the age string. However, the parse action takes
place inside the try block. If the call of Parse throws an exception the code in the
catch block runs and will display a message to the user. Note that once the exception
has been thrown there is no return to the code in the try block, i.e. if the parse fails the
program will not display the message "Thank you".

以上是关于csharp C#使用TRY捕获异常的主要内容,如果未能解决你的问题,请参考以下文章

csharp 有关如何使用try catch构造捕获异常的示例。从计算机编程基础与C#http://www.introprogr

csharp C#异常TRY和CATCH

try使用多个catch捕获不同的异常

try使用多个catch捕获不同的异常

VC++异常捕获__try...__except和try...catch的使用介绍(附源码)

MFC TRY CATCH 与 C++ 尝试使用 MFC 捕获异常