November 2007
Beginner
954 pages
25h 7m
English
You can also define your own exception objects to represent custom error conditions. All you need to do is create an instance of the appropriate exception class and then use the throw statement.
The next example introduces a modified DivideNumbers() method. It explicitly checks whether the specified divisor is 0 and then manually creates and throws an instance of the DivideByZeroException class to indicate the problem, rather than attempt the operation. Depending on the code, this pattern can save time by eliminating some unnecessary steps, or it can prevent a task from being initiated if it can't be completed successfully.
protected void Page_Load(Object sender, EventArgs e) { try { DivideNumbers(5, 0); } catch ...Read now
Unlock full access