powerpoint 10

Download Report

Transcript powerpoint 10

CSC 298
Exception Handling
C# 1
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.
C# 2
throw statement
 Use a throw statement to signal to the user an
abnormal condition
 e.g.
public double Sqrt(double x)
{
if (x<0)
throw new ArgumentException(
"Negative value x=" + x);
else
// Note: Math.Sqrt(-10) is NaN
return Math.Sqrt(x);
}
C# 3
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);
}
C# 4
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
C# 5
catch clause?
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
C# 6
aborts.
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).
C# 7
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 */ }
C# 8
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 e1
throw e2;
}
C# 9
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).
C# 10