|
To | Do this |
---|---|
Generate an exception. | Use the throw keyword, using a pointer to a managed type as the argument. For example: throw new SomeException(); |
Catch an exception. | Use the try and catch construct, surrounding the code that might fail with a try block, followed by one or more catch blocks. For example: try { // code that might fail } catch(SomeException* pce) { // handle the exception } |
Catch more than one exception. | Chain catch blocks together. For example: catch(SomeException* pce) { // handle the exception } catch(SomeOtherException* pce) { // handle the exception } |
Catch a family of | Use the base class of the exceptions you want to catch in the catch block; for example, ArithmeticException will catch DivideByZeroException and several others. |
Catch every exception. | Use a catch block that takes Exception* as a parameter, which will catch every type that is derived from Exception. |
Handle exceptions at more than one point in a program. | Use throw to rethrow exceptions from one catch block to another. |
Create your own | Derive from the Exception class, adding your own members. |
|