csharp C#异常TRY和CATCH

Posted

tags:

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

int age;
try
{
 age = int.Parse(ageString);
 Console.WriteLine("Thank you");
}
catch (Exception e)
{
 // Get the error message out of the exception
 Console.WriteLine(e.Message);
}



The catch now looks like a method call, with the Exception e being a parameter to
the method. This is exactly how it works. Within the catch clause the value of e is set
to the exception that was thrown by Parse. The Exception type has a property called
Message which contains a string describing the error. If a user types in an invalid
string the program above will write the text in the exception, which contains the
message:

Input string was not in a correct format.


This message is obtained from the exception that was thrown. This is useful if the code
inside the try block could throw several different kinds of exception, since it means
that the message reflects what has actually hap

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