1 / 11

Exception Handling

Exception Handling. Chapter 13 Dr. Abraham. Pseudocode. Perform task If the preceding task did not execute correctly perform error processing Perform next task If the preceding task did not execute correctly perform error processing

raub
Download Presentation

Exception Handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exception Handling Chapter 13 Dr. Abraham

  2. Pseudocode • Perform task • If the preceding task did not execute correctly perform error processing • Perform next task • If the preceding task did not execute correctly perform error processing • --allows programmer to remove error-handling from main-line of the program execution--

  3. Example of exception hierarchy • Exception • formatException • ArithmeticException • Overflow Exception • Divideby Zero Exception • All exceptions are subclasses of the Exception class. • An application can communicate with its users through a messageBox.

  4. Catching an exception • Try-catch blocks. • Place try block around statements that might create an exception. • Exception Handler: Immediately after the try block, code a catch block that contains statements that would be executed if an exception occurred.

  5. Example program I wrote: • I created four separate methods to do a simple calculation. The first one without any error handler. The second, with error handler to catch division by zero. The third, to handle format error caused by converting one type to another. The fourth combining the two known exceptions and anything else. • I explain the code in class. You may be required to do something similar on the exam. So, pay attention.

  6. More than one exceptions thrown by try block • You can have multiple catch blocks to handle each error personalized by you. • You can have catch block to simply display windows messages.

  7. Finally block • Finally block after all the catch blocks to code for things to do whether error occurred or not. This is usually used to free any system resources. • It is used to prevent resource leaks (example, files left open) and memory leak s (example, memory not deallocated).

  8. Throwing exceptions • You can throw exceptions created by you. • Through keyword followed by new keyword: If (monthlyInvestment <=0) Throw new Exception(“Monthly Investment must be greater than 0”);

  9. When to throw • When a method encounters a situation where it is not able to complete its task • When you want to catch the eception and perform some processing then through the exception again

  10. Termination and Resumption Models • When CLR detects a problem an exception is thrown. At the point the exception is detected is called the throw point. • The control now transfers to the catch block. After the catch block the control does not transfer back to the throw point automatically. The program resumes after the catch blocks. This is known as the Termination Model of Exception Handling. • C# does not allow you to resume at the next statement after the throw point. Windows Form based programs don’t need to resume.

More Related