Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exceptions COMP53 Sept 7 2007. Exceptions An exception is an object that gets thrown to indicate an error or other exceptional condition. Using exceptions.

Similar presentations


Presentation on theme: "Exceptions COMP53 Sept 7 2007. Exceptions An exception is an object that gets thrown to indicate an error or other exceptional condition. Using exceptions."— Presentation transcript:

1 Exceptions COMP53 Sept 7 2007

2 Exceptions An exception is an object that gets thrown to indicate an error or other exceptional condition. Using exceptions allows for a clean way to escape from a problem, without complication the normal control logic. Throwing an exception says: “I can’t do this, so I’m throwing the ball back to someone else.

3 Exception Metaphor Bill passes a task to Jane Jane passes the task on to Jerry Jerry has a problem with the task, so he throws it back to Jane. If Jane can handle the problem, she does so, otherwise she throws it back to Bill. If Jane can handle the problem, he does so, otherwise he gives up on it.

4 Exception Objects Java provides a general purpose Exception class. – Often this is sufficient We can also define special purpose Exceptions as subclasses of Exception – This allows us to be more selective in handling exceptions

5 Creating an Exception Class public class DivideByZeroException extends Exception { DivideByZeroException(String _msg) { super(_msg); } All exceptions can carry a message (String) to give information back to the receiver of the exception.

6 Throwing Exceptions The throw command is used to throw exceptions. A new exception object must be created when it is thrown. Exceptions that might be thrown by a method must be identified in the method signature.

7 Method Throwing an Exception private static double divideThese(double x, double y) throws DivideByZeroException { if (y==0.0) throw new DivideByZeroException("in divideThese()"); double z = x/y; return z; } If the exception is thrown, control is immediately returned to the method that called this method.

8 Catching an Exception Anyone calling a method that might throw an exception must be prepared to catch it. This requires a try-catch block. – Try to do something that might throw an exception – If there is no exception, proceed normally – If there is an exception, immediately jump to code that will deal with it.

9 Catching an Exception try { double a = divideThese(10,0); System.out.println("Answer is "+a); } catch (DivideByZeroException dbz) { System.out.println(dbz); } If a DivideByZeroException is thrown during the call to divideThese(), control will immediately jump to the code in the catch block.

10 When to Use Exceptions We’ll primarily use exceptions to indicate that something has gone wrong inside of a data structure. Examples: – an attempt to access an array with an out of bounds index – trying to extract (pop) an object from an empty queue


Download ppt "Exceptions COMP53 Sept 7 2007. Exceptions An exception is an object that gets thrown to indicate an error or other exceptional condition. Using exceptions."

Similar presentations


Ads by Google