The try-catch statement consistsTryBlock and one or moreCatchClause (specify processing for different exceptions)Program. This statement takes one of the following forms:
Try Try-blockCatch (Exception-declaration-1) Catch-block-1Catch (Exception-declaration-2) Catch-block-2...Try Try-blockCatch Catch-block
Where:
-
-
Try-block
-
-
Include exceptions that should be thrown Code.
-
-
Exception-Declaration,
Exception-declaration-1,
Exception-declaration-2
-
-
Exception object declaration.
-
-
Catch-block,
Catch-block-1,
Catch-block-2
-
-
Contains the exception handling program.
Example
In this example, two catch statements are used. The most specific exception that occurs first is caught.
// Ordering catch clauses
Using system;
Class myclass
{
Public static void main ()
{
Myclass x = new myclass ();
Try
{
String S = NULL;
X. myfn (s );
}
-
-
// Most specific:
Catch (argumentnullexception E)
{
Console. writeline ("{0} first exception caught.", e );
}
-
-
// Least specific:
Catch (exception E)
{
Console. writeline ("{0} second exception caught.", e );
}
-
-
}
-
-
Public void myfn (string S)
{
If (S = NULL)
Throw new argumentnullexception ();
}
}
Output
System. argumentnullexception: value cannot be null.
At...
At... first exception caught.