Public void mymothed ()
{
Try
{
// Do something
Int x = 5/0; // It is divided by 0. An error occurs here.
}
Catch (systemexception)
{
// Do something here
}
Catch (dividebyzeroexception)
{
// Here is the solution to the error of division by 0
}
Catch (exception)
{
// This is a general Catch Block"
}
}
In the above example, the last "catch (exception)" is "General Catch Block", which is responsible for capturing exceptions, the exception type indicates "errors occurred during application execution. ", That is to say, no matter what error occurs, this catch can catch it. All error types in C # are derived from this exception class, and the C # syntax specifies a try .. only one common catch block can be found.
In addition, the catch (exception) block must be placed in a try .. at the end of the catch structure, otherwise a compiler warning will be generated, because this block will capture all the errors, so the subsequent catch statements will never be executed.