|
.NET Exception handling - August 25, 2008 at 18:00 PM by Amit
Satpute
Describe how exceptions are handled by the CLR?
Answer
Usually the exceptions that occur in the try are caught in the catch
block. The finally is used to do all the cleaning up work. But exceptions can
occur even in the finally block. By using CLR, the exception are caught even in
the finally block. Also when an exception is thrown, the CLR looks for an
appropriate catch filter that can handle the exception and then it executes the
finally block before terminating the execution on the catch
filter.
How to throw a custom exception?
Answer
The usual try - catch - finally - ent try format has to be followed.
However, in this case, instead of using the preset exceptions from the
System.Exception, you define your OwnException class and inherit Exception or
ApplicationException class.
You need to define three constructors in your OwnException Class: One without
parameters, other with String parameter for error message and the last one has
to have one parameter as a String and other as an Inner exception object.
Example:
class OwnException : ApplicationException
{
public OwnException() : base() {}
public OwnException(string s) : base(s) {}
public OwnException(string s, Exception ae) : base(s,
ae) {}
}
|