When a bad behavior occurs in your application, the program is said to throw an exception. To customize the throwing of an exception, in the section of code where you are anticipating the error, type the throw keyword followed by a new instance of the Exception class (or one of its derived classes) using the constructor that takes a string. Here is an example: <script runat="server"> private void btnCalculateClick(object sender, EventArgs e) { double side; double perimeter, area; try { side = double.Parse(txtSide.Text); throw new Exception(side.ToString()); perimeter = side * 4; area = side * side; txtPerimeter.Text = perimeter.ToString(); txtArea.Text = area.ToString(); } catch(Exception ex) { lblMessage.Text = "The value you typed, " + ex.Message + " is not valid."; } } </script> In reality, to effectively throw an exception, you should write a conditional that specifies under what circumstance(s) the exception should be thrown. A function or a method with numerous or complex operations and requests can also produce different types of errors. To catch various exceptions, you can create different catch sections, each made for a particular error. The formula used would be: try { // Code to Try } catch(Arg1) { // One Exception } catch(Arg2) { // Another Exception } This makes it to create a catch block for each type of exception. In the same way, you can write different throw expressions if the code can produce different types of errors.
You can create an exception inside of another. This is referred to as nesting an exception. To nest an exception, write a try block in the body of the parent exception. The nested try block must be followed by its own catch(es) clause. To effectively handle the exception, make sure you include an appropriate throw in the try block.
One of the most effective techniques used to deal with code is to isolate assignments. Isolating assignments and handing them to method is an important matter in the area of application programming. One of the ways you can use functions or methods in exception routines is to have a central method that receives variables, and sends them to other external methods. The external method tests the value of a variable. If an exception occurs, the external method displays or sends a throw. This throw can be picked up by the method that sent the error.
The Exception class of the .NET Framework is a great tool for handling exceptions. To deal with particular errors, various classes are derived from Exception. If for some reason neither the Exception class nor any of the Exception-based classes fulfills your requirement, you can derive a new class from Exception or from one of the available Exception-based classes. To derive a class from Exception or from one of its classes, simply follow the rules of class inheritance. Here is an example of a class based on Exception: public class CustomException : Exception { } After deriving the class, you can add the necessary members as you see fit. Remember that the primary characteristic of an exception is to present a message to the user. In Exception-based classes, this message is represented by the Message property. Therefore, if you want to prepare a custom message for your class, you can override or new this property. Once you have created and customized your exceptional class, you can use it the same way you would another exception class, such as throwing it. |
|
|||||||||||||||||
|