Article Directory
|
Exceptions are problems that occur during program execution. An exception in C# is a response to a special situation that occurs when the program is running, such as trying to divide by zero.
Exceptions provide a way to transfer control of a program from one part to another. C# exception handling is based on four keywords: try, catch, finally, and throw.
try : A try block identifies a specific exception code block that will be activated. Followed by one or more catch blocks.
catch : The program catches the exception through the exception handler. The catch keyword represents the catch of an exception.
finally : finally block is used to execute a given statement, regardless of whether an exception is thrown will be executed. For example, if you open a file, it will be closed regardless of whether there is an abnormality.
throw : When a problem occurs, the program throws an exception. Use the throw keyword to complete.
Assuming that an exception will occur in a block, a method uses try and catch keywords to catch the exception. The code in the try/catch block is protected code, using the try/catch syntax is as follows:
try
{
// 引起异常的语句
}
catch( ExceptionName e1 )
{
// 错误处理代码
}
catch( ExceptionName e2 )
{
// 错误处理代码
}
catch( ExceptionName eN )
{
// 错误处理代码
}
finally
{
// 要执行的语句
}
C# exceptions are expressed using classes. The exception class in C# is mainly derived directly or indirectly from the System.Exception class.
-
The System.ApplicationException and System.SystemException classes are exception classes derived from the System.Exception class.
-
The System.ApplicationException class supports exceptions generated by applications. So programmer-defined exceptions should be derived from this class.
-
The System.SystemException class is the base class for all predefined system exceptions.
|
|