Download presentation
Presentation is loading. Please wait.
Published byClare Mitchell Modified over 9 years ago
1
Class Design: Handling Errors Reading: 2 nd Ed: Chapter 15 3 rd Ed: Chapter 11 Exercises 2 nd Ed: P15.5, P15.6 (Hint: look at documentation for Scanner class!) 3 rd Ed: P11.9, P11.11 You will be expected to: incorporate exception handling into the design of a method's contract trace code that makes use of exception handling write code to throw, catch or propagate an exception write code that uses a finally block write code to define a new exception class compare and contrast checked and unchecked exceptions understand the consequence of using checked vs. unchecked exceptions
2
Class Design - Exceptions 2 Exceptions – Why do we need them? Remember the Account class? We added the following precondition to the deposit method: amount >= 0 What if the client fails to check the precondition? The customers won't be happy to find out that sloppy programming has resulted in losing money because of a simple mistake! MAIN POINT … The method where a problem is detected, is often not the method which can resolve the problem
3
Class Design - Exceptions 3 Exceptions – Why we need them? If a precondition is violated, we can have the method: return a special value (e.g., true/false) to indicate whether or not the operation was successful problem: print an error message problem: terminate the program problem:
4
Class Design - Exceptions 4 Exceptions – Why we need them? Instead, we can have the method throw an exception if the amount is negative. Benefits: We can neatly separate regular code from error handling code We can force the client to acknowledge the problem. We allow the client to decide how to handle the problem.
5
Class Design - Exceptions 5 What's a Java Exception? An exception is an object with a specific interface, that can be thrown. All exception classes are subclasses of the class Throwable defined in the Java library. Here are some of the methods of this class: Throwable(); Throwable( String message ); String getMessage(); void printStackTrace(); Exceptions encapsulate information about the kind of problem that has occurred (the message) and the stack of method calls that led to the problem (the stack trace).
6
Class Design - Exceptions 6 What's an exception? There are two types of exception: checked and unchecked. Checked exceptions are handled specially by the Java compiler. This provides warnings and reminders about exceptions to programmers. Methods that might throw checked exceptions, must declare it in their signature Methods that call another method throwing a checked exception must either: Catch the exception Also declare it in their signature
7
Class Design - Exceptions 7 What's an exception? Unchecked exceptions are subclasses of Java’s RuntimeException or Error class, while all others are checked exceptions. Methods that might throw unchecked exceptions, do not need to declare it in their signature Methods that call another method throwing an unchecked exception can optionally: Catch the exception If a method does not catch an exception, than the exception propagates to the method’s caller, and so on…. Error represents abnormal conditions that a program would normally not be expected to handle.
8
Class Design - Exceptions 8 Java Exception Hierarchy Numerous exceptions and errors are defined in various java packages. i.e., FileNotFoundException in java.io IOException in java.io NullPointerException in java.lang etc. Programmers can define their own exceptions as subclasses of Exception or its subclasses. Object Throwable Exception Error RunTimeException checked exceptions unchecked IOException
9
Class Design - Exceptions 9 Defining an Exception Class Returning to our Account example, suppose we decide to throw an exception when the amount is negative. First we must decide which exception class to use. We could use the class Exception in the Java library but we can capture more information by defining our own exception class. Let's define a class named IllegalValueException to represent the type of exception that will be thrown when we attempt to pass a negative amount. This will be a checked exception (more about this later).
10
Class Design - Exceptions 10 Defining an exception class public class IllegalValueException extends Exception { public IllegalValueException() { } public IllegalValueException(String msg) { super(msg); }
11
Class Design - Exceptions 11 Checked versus Unchecked … Why? Checked exceptions usually represent problems which are expected to occur, so they must be handled e.g. incorrect password Unchecked exceptions represent bugs in a method SO … explicitly declaring an unchecked exception in a method signature does not make sense … WHY?
12
Class Design - Exceptions 12 Throwing an Exception /** * Deposit money into the account * @param amount The amount to be deposited * * @pre true * @post IF amount >= 0 * THEN getBalance() = @pre.getBalance() + amount * ELSE getBalance() = @pre.getBalance() * @throws IllegalValueException if amount is negative */ public void deposit(double amount) throws IllegalValueException { if (amount < 0) throw new IllegalValueException("Error: Neg. amount"); balance = balance + amount; }
13
Class Design - Exceptions 13 Catching Exceptions Recall that IllegalValueException is a checked exception. This has consequences for a client calling our deposit method. The client code must do one of the following: catch the exception propagate (i.e., pass on) the exception to its caller (i.e., the method that called it) The type of exception thrown must be a sub-type (or equal type) as the catch parameter type
14
Class Design - Exceptions 14 Try/Catch Block try { //Normal code to execute //Can include other nested try/catch blocks } catch(ExceptionType1 name) { //Code to execute if exception type 1 is thrown in normal code } catch(ExceptionType2 name) { //Code to execute when exception type 2 is thrown in normal code } … possibly more catch blocks
15
Class Design - Exceptions 15 Client Catching an Exception public static void main( String[] args ) { Account instructorAccount = new Account ( “instructor”, 100.0 ); try { instructorAccount.deposit( 100 ); System.out.println( "Balance: " + instructorAccount.getBalance() ); } catch( IllegalValueException e ) { System.out.println( e.getMessage() ); } } What happens when deposit is called?
16
Class Design - Exceptions 16 What happens when this code executes? public static void main( String[] args ) { Account instructorAccount = new Account ( “instructor”, 100.0 ); try { instructorAccount.deposit( -100 ); System.out.println( "Balance: " + instructorAccount.getBalance() ); } catch( IllegalValueException e ) { System.out.println( e.getMessage() ); } }
17
Class Design - Exceptions 17 Client Propagating an Exception public void depositToAccount( Account anAccount, double amount ) throws IllegalValueException { anAccount.deposit( amount ); System.out.println( "Balance: " + anAccount.getBalance() ); } The method that calls deposit must either: catch the exception propagate the exception to its caller If it propagates the exception then its caller must either catch or propagate the exception and so on…
18
Class Design - Exceptions 18 Tracing an example call Trace the following code: public static void main( String[] args ) { Account anAccount = new Account ( “test”, 200 ); try { depositToAccount( anAccount, 100.0 ); } catch( IllegalValueException e ) { System.out.println( e.getMessage() ); } }
19
Class Design - Exceptions 19 Tracing an example call Trace the following code: public static void main( String[] args ) { Account anAccount = new Account ( “test”, 200 ); try { depositToAccount( anAccount, -100.0 ); } catch( IllegalValueException e ) { System.out.println( e.getMessage() ); } }
20
Class Design - Exceptions 20 Exception Propagation If the exception is propagated as far as main() and main() doesn't catch the exception, the program is terminated. The error message associated with the exception is printed on the screen along with the stack trace. Allowing your program to terminate when an exception is thrown is not acceptable in real software (and could lead to disaster in real code!)
21
Class Design - Exceptions 21 Unchecked Exceptions If a method throws an unchecked exception, the rules are different: it is not necessary to declare that the method throws the exception there is no requirement on the calling method to handle the exception (i.e., doesn’t have to catch or explicitly declare the exception in its signature) Assertion failures throw unchecked exceptions A good design is to use a global “safety-net” handler in “main” To handle all unforeseen circumstances
22
Class Design - Exceptions 22 Example public class ExamMarker { //... /** * Calculates the given mark as a percentage of max mark * @param mark the given mark * @param max the maximum mark for the exam * @return the mark as a percentage of max */ public int percentage(double mark, double max) throws IllegalMarkException, IllegalMaxException { if ( max == 0 ) throw new IllegalMaxException( "Max is 0" ); if( mark max ) throw new IllegalMarkException( "Incorrect Mark Submitted" ); return (int)( mark / max * 100 ); }
23
Class Design - Exceptions 23 public static void main(String[] args) { ExamMarker marker = new ExamMarker(); Scanner input = new Scanner( System.in ); double mark, max; int percent; System.out.println( "Enter a mark for this exam and the max mark: " ); // cont'd
24
Class Design - Exceptions 24 while( input.hasNext() ) { mark = input.nextDouble(); max = input.nextDouble(); try { percent = marker.percentage( mark, max); System.out.println( "The exam mark is: " + percent + "%" ); } catch( IllegalMaxException e ) { System.out.println( "Exam Marker Error: " + e.getMessage() ); } catch( IllegalMarkException e ) { System.out.println( "Exam Marker Error: " + e.getMessage() ); }
25
Class Design - Exceptions 25 public class ExamMarkerException extends Exception { public ExamMarkerException(){ } public ExamMarkerException( String msg ) { super(msg); }
26
Class Design - Exceptions 26 public class IllegalMarkException extends ExamMarkerException { public IllegalMarkException(){ } public IllegalMarkException( String msg ) { super( msg ); } public class IllegalMaxException extends ExamMarkerException { public IllegalMaxException(){} public IllegalMaxException( String msg ) { super( msg ); }
27
Class Design - Exceptions 27 Question What will be output if we enter the following data on the keyboard? 20.0 50.0 40.0 30.0 Give an example of data that we could enter on the keyboard to cause an IllegalMaxException to be thrown.
28
Class Design - Exceptions 28 Question What if we replace the two catch blocks in main() with the following? catch( ExamMarkerException e ) { System.out.println( "Exam Marker Error: " + e.getMessage() ); } How will the output change?
29
Class Design - Exceptions 29 Comments Note that methods can throw more than one type of exception. If we call a method that throws more than one type of exception we can have more than one catch block to handle each type of exception. Catch blocks must be ordered from the most specific type of exception (the one lowest in the inheritance hierarchy) to the least specific (the one highest in the hierarchy).
30
Class Design - Exceptions 30 Designing Exceptions Need to distinguish boundary cases that can be handled by the method from exceptional cases which should throw exceptions Define individual exception for each type of error can group them into hierarchies – allows more flexibility in handling them Exceptions thrown by a method are shown in the method’s comment using the @throws tag. Too many exceptions may make the method difficult to use.
31
Class Design - Exceptions 31 Example: Class Account Re-designed We redesign deposit and withdraw to throw exceptions in the error cases /** * A simple bank account for which the balance can never be * less than zero * * @invariant getBalance() >= 0 * @invariant getId()is unique and set when account is created * @invariant getName()is set when account is created * @invariant the values of getId() and getName() never change */ public class Account { private int id; private static int nextAccountId = 0; private String name; private double balance;.....
32
Class Design - Exceptions 32 /** * Deposit money into the account * @param amount The amount to be deposited * * @pre amount >= 0 * @post getBalance() = @pre.getBalance() + amount * @return The current balance of the account */ public double deposit(double amount) { assert amount >= 0; balance = balance + amount; return balance }
33
Class Design - Exceptions 33 /** * Deposit money into the account * @param amount The amount to be deposited * * @pre true * @post IF amount >= 0 * THEN getBalance() = @pre.getBalance() + amount * ELSE getBalance() = @pre.getBalance() * @return The current balance of the account * @throws IllegalValueException if amount is negative */ public double deposit(double amount) throws IllegalValueException { if (amount < 0) throw new IllegalValueException("Error: Neg. amount"); balance = balance + amount; return balance } Should it have “assert”? YES NO Another Design for Deposit
34
Class Design - Exceptions 34 /** * Withdraw money from the account * @param amount The amount to be withdrawn * @pre true * @post IF (amount >= 0 AND @pre.getBalance()-amount >= 0 ) * THEN getBalance() = @pre.getBalance() - amount * ELSE getBalance() = @pre.getBalance() * @return The current balance of the account * @throws IllegalValueException if amount<0 * @throws NotEnoughMoneyException if getBalance()-amount<0 */ public double withdraw(double amount) throws IllegalValueException, NotEnoughMoneyException { if (amount < 0) throw new IllegalValueException("Error: Neg. amount"); if (balance - amount < 0) throw new NotEnoughMoneyException("Error: no $$$"); balance = balance - amount; return balance; }
35
Class Design - Exceptions 35 /** * Returns the string representation of an account * * @pre true * @return the account represented as a string */ public String toString() { return "[ id = " + id + ", name = " + name + ", balance = " + balance + "]"; }
36
Class Design - Exceptions 36 Account Exceptions public class IllegalValueException extends AccountException { public IllegalValueException() {} public IllegalValueException(String msg) { super(msg); } public class NotEnoughMoneyException extends AccountException { public NotEnoughMoneyException() {} public NotEnoughMoneyException(String msg) { super(msg); } NOTE: We could use Java’s IllegalArgumentException instead of defining a new exception for illegal value
37
Class Design - Exceptions 37 Exercise class Weather { String sunshine(String s) throws SunException, RainException { if (s != null) { if (s.equals("Strong sun")) { return "Better use sunblock!"; } throw new SunException( “It won’t last long."); } throw new RainException("No sun today."); } void fog(String x) { try { System.out.println(snow(x)); } catch (ColdException ce) { System.out.println( “You should expect“ + ce.getMessage); } String snow(String s) throws ColdException { if (s != null && s.equals("Really cold")) { throw new ColdException(“dry snow"); } try { return sunshine(s); } catch (RainException re){ return "Terrible! “ + re.getMessage(); } catch (SunException se) { return " Don't worry “ + se.getMessage(); } Assuming that the exceptions used here are appropriately defined, what would the following calls produce? new Weather().fog(“Showers”); new Weather().fog(“Really Cold”); new Weather().fog(“Strong Sun”); new Weather().fog(null);
38
Class Design - Exceptions 38 The finally clause A finally clause can follow the catch clauses of a try block (or even a try block with no catch clauses): try { // code that may throw checked exceptions } catch( SomeException e ) { … } finally { … } The finally clause is executed whether or not an exception is thrown, and (if thrown) whether or not it was caught. It is often used to ensure that resources are released.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.