1 / 10

CSC 298

CSC 298. Exception Handling. Motivation. We want to prevent our program from giving wrong answers or worse crashing. Standard approach: check any values used for a computation (e.g., is the grade between 0 and 4?)

wattan
Download Presentation

CSC 298

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. CSC 298 Exception Handling

  2. Motivation • We want to prevent our program from giving wrong answers or worse crashing. • Standard approach: check any values used for a computation (e.g., is the grade between 0 and 4?) • However, sometimes, we have no control over circumstances that might affect our program • What if the disk drive fails while writing to it? • Exception handling: a facility to allow the programmer to • insert code to handle unexpected errors • allow the program to recover and continue executing, or terminate gracefully.

  3. throw statement • Use a throw statement to signal to the user an abnormal condition • e.g. public double Sqrt(double x) { if (x<0) thrownew ArgumentException( "Negative value x=" + x); else // Note: Math.Sqrt(-10) is NaN return Math.Sqrt(x); }

  4. try – catch statement (1) • Enclose in a try block any code that might throw an exception. If an exception is thrown, the control flow is transferred to the appropriate catch block following the try block. • catching an exception try { Console.WriteLine(Sqrt(-10)); } catch (ArgumentException ae) { Console.WriteLine(ae.Message); }

  5. try and catch (2) • Write in the try block the code that might throw exceptions (different types of exception might be thrown). • Write in one or more catch clauses the code to handle the exceptions try{ //statements that might throw exceptions } catch(ExceptionType1 e1){/*handling code*/} catch(ExceptionType2 e2){/*handling code*/} • What if the type of the exception is not listed in the catch clauses? • What happens once we are done with the catch clause?

  6. Control flow for exceptions • When an exception is thrown in a try block • the control flow goes to the first catch block listed after the try block that matches the exception type (same class or a superclass) • Once done with the catch clause, execution resumes with the first statement after the list of the catch blocks. Execution doesn't go back in the try block. • If no matching catch is found in the method, the CLR looks for a matching catch in the caller of the method. The process goes on until a catch is found. • If no catch is found, the program or thread aborts.

  7. catch { } • Don't use any argument in the catch to catch all exceptions (but don't get any information about the exception) try{ //statements that might throw //exceptions } catch { /*all exceptions are caught here*/ } • Always write catch blocks from the most specific to the least specific (catch {} should always be last).

  8. finally statement • Sometimes some operations must be done before leaving a method (e.g. closing a connection to a database). What if the code leaves abruptly the method by throwing an exception? • Code written within a finally block is always executed (whether an exception is thrown or not) try{} catch (...){} finally{ /* always done */ }

  9. Exception classes • Check "exceptions, hierarchies" (within the help index of Visual studio). • Exception class: base class of all exceptions. • Some properties of exception objects • Message: a String describing the exception • StackTrace: a String that the chain of calls on the stack when the exception was thrown. • InnerException: it may be possible to wrap an exception within an exception (e.g. when rethrowing an exception)catch(ExceptionType1 e1){ ExceptionType2 e2 = new ExceptionType2 ("message", e1); //if such constructor// e2.InnerException is e1throw e2; }

  10. Creating your own exceptions • Extend the ApplicationException class and add your own custom methods and properties. • ApplicationException is never thrown by the CLR (allows to differentiate between exceptions thrown by the system, and exceptions thrown by a user program).

More Related