08 1
play

08-1 Custom Exception Classes Throwable/Exception Hierarchy To - PDF document

Review Can throw exceptions to indicate unusual or erroneous conditions CSC 143 Java Create an object to hold data about the kind of exception Can be an instance of any subclass of Throwable Throw it More on Exceptions


  1. Review • Can throw exceptions to indicate unusual or erroneous conditions CSC 143 Java • Create an object to hold data about the kind of exception • Can be an instance of any subclass of Throwable • Throw it More on Exceptions • Can catch exceptions of desired kinds • Exceptions help to separate the error-handling code from the regular code 1 2 Generic Exceptions Custom Exceptions • So far, only throw exceptions of standard exception classes • A better way would be to create a special kind of exception, [such as??] just for the error condition that's being reported private void updateBalance( double amount) { private void updateBalance( double amount) { if (this.balance + amount < 0) { if (this.balance + amount < 0) { throw new IllegalArgumentException("insufficient funds"); throw new InsufficientFundsException( ); } else { } else { this.balance = this.balance + amount; this.balance = this.balance + amount; } } } } • To catch this error, must catch all IllegalArgumentExceptions... any drawbacks??? • Think of two reasons why that's good... 3 4 08-1

  2. Custom Exception Classes Throwable/Exception Hierarchy • To create a custom exception: Throwable Error Exception public class InsufficientFundsException extends RuntimeException { public InsufficientFundsException( ) { RuntimeException ... // invoke RuntimeException's constructor to set the error message ArithmeticException super("insufficient funds"); } NullPointerException } IllegalArgumentException InsufficientFundsException •Exception subclass is a regular class •may have instance variables, constructors, methods, etc. etc. 5 6 Handling Custom Exceptions Declaring Thrown Exceptions • A method can declare the exceptions that it might throw, either • As expected: directly or indirectly public void withdraw(double amount) throws InsufficientFundsException { try { this.updateBalance(- amount); mySavingsAccount.withdraw(100.00); } myCheckingAccount.deposit(100.00); private void updateBalance(double amount) throws InsufficientFundsException } catch (InsufficientFundsException exn) { { if (this.balance + amount < 0) { System.out.println("Transaction failed: " + exn.getMessage()); throw new InsufficientFundsException( ); } } else { this.balance = this.balance + amount; • Now won't accidentally catch unrelated } } IllegalArgumentExceptions 7 8 08-2

  3. Throws Clauses throw vs. throws • Syntax • Two separate keywords in Java < returnType > < messageName >(< arguments >) • Make sure you know the difference! throws < ExceptionClass1 >, …, < ExceptionClassN > • For regular methods, abstract methods, methods in • Hint: where can you use each of them?? interfaces, and constructors • Informs callers what exceptions they might need to handle • It's good practice for a method to either handle all exceptions that may be raised by the messages it sends, or document using throws clauses that some exceptions might be propagated out of it to its callers 9 10 Checked vs. Unchecked Exceptions Throwable/Exception Hierarchy • For exception classes that are subclasses of Throwable RuntimeException, throws clauses are optional Error Exception • The idea is that these exceptions might be so frequently thrown, [checked] RuntimeException [checked] that it would be pointless to list them everywhere IOException • Called unchecked exceptions [checked] ArithmeticException [checked] FileNotFoundException • But for other exceptions, e.g. subclasses of Exception other NullPointerException [checked] than RuntimeException, throws clauses are required • Called checked exceptions [checked] IllegalArgumentException InsufficientFundsException 11 12 08-3

  4. Custom Checked Exceptions Throws Clauses and Overriding • Java requires: all checked exceptions be handled, or be listed in the • When one method overrides another throws clause • old rule: it must have the same signature (argument and result • Good practice (according to some programmers): custom exceptions types) should be checked exceptions • new rule: it must have the same signature and it must include the public class InsufficientFundsException extends Exception { ... } “same” exceptions in its throws clause (“same” since derived types are also OK) public void withdraw( double amount) throws InsufficientFundsException {…} private void updateBalance( double amount) throws InsufficientFundsException { … } • One consequence: when declaring a general interface, e.g. public void transferTo(BankAccount other, double amount) for all Shapes, each method in the interface must mention all // no throws clause needed, since all possible checked exceptions handled { exceptions that any overriding method implementation might try { this.withdraw(amount); other.deposit(amount); } throw catch (InsufficientFundsException exn) { … } • This can be tricky to get right } 13 14 08-4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend