Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 11: Exception Handling Exceptions and Exception Types Exceptions and Exception Types Claiming Exceptions Claiming Exceptions Throwing Exceptions.

Similar presentations


Presentation on theme: "Chapter 11: Exception Handling Exceptions and Exception Types Exceptions and Exception Types Claiming Exceptions Claiming Exceptions Throwing Exceptions."— Presentation transcript:

1 Chapter 11: Exception Handling Exceptions and Exception Types Exceptions and Exception Types Claiming Exceptions Claiming Exceptions Throwing Exceptions Throwing Exceptions Catching Exceptions Catching Exceptions Creating Your Own Exception Classes Creating Your Own Exception Classes Rethrowing Exceptions Rethrowing Exceptions The finally Clause The finally Clause Cautions When Using Exceptions Cautions When Using Exceptions

2 Exceptions and Exception Types

3 Claiming Exceptions public void myMethod() public void myMethod() throws IOException throws IOException public void myMethod() public void myMethod() throws IOException, OtherException throws IOException, OtherException

4 Throwing Exceptions throw new TheException(); throw new TheException(); TheException e = new TheException(); throw e; TheException e = new TheException(); throw e;

5 Throwing Exceptions Example public Rational divide(Rational r) throws Exception { if (r.numer == 0) if (r.numer == 0){ throw new Exception("denominator throw new Exception("denominator cannot be zero"); cannot be zero"); } long n = numer*r.denom; long n = numer*r.denom; long d = denom*r.numer; long d = denom*r.numer; return new Rational(n,d); return new Rational(n,d);}

6 Catching Exceptionstry{ statements; statements;} catch (Exception1 e) { handler for exception1 } catch (Exception2 e) { handler for exception2 }... catch (ExceptionN e) { handler for exceptionN }

7 Example 11.2 Catching Exceptions Objective: Write a program to test the new Rational class. Objective: Write a program to test the new Rational class. TestRationalException Run Rational

8 Exceptions in GUI Applications An error message appears on the console, but the GUI application continues running. An error message appears on the console, but the GUI application continues running. Re-run the MenuDemo applet from Example 9.9 and divide by 0 to see how a GUI deals with unhandled exceptions. Re-run the MenuDemo applet from Example 9.9 and divide by 0 to see how a GUI deals with unhandled exceptions. MenuDemo Run

9 Example 11.4 Creating Your Own Exception Classes F Objective: Create 10 accounts and transfer funds among the accounts. Raise exceptions if a transaction amount is negative or the account balance is less than the transaction amount. TestMyExceptionRun NegativeAmountExceptionAccount InsufficientFundException

10 Example 11.5 Using Exceptions in Applets Objective: An applet for account transactions that displays account ID and balance and performs deposits and withdrawals. For each transaction, a message indicates success or failure of the transaction, and reports any reason for failure. Objective: An applet for account transactions that displays account ID and balance and performs deposits and withdrawals. For each transaction, a message indicates success or failure of the transaction, and reports any reason for failure. AccountApplet Run Applet Viewer

11 Rethrowing Exceptionstry{ statements; statements;} catch(TheException e) { perform operations before exits; perform operations before exits; throw e; throw e;}

12 The finally Clausetry{ statements; statements;} catch(TheException e) { handling e; handling e;}finally{ finalStatements; finalStatements;}

13 Cautions When Using Exceptions Example 11.6: Demonstrating Performance Differences With and Without Exception Handling Example 11.6: Demonstrating Performance Differences With and Without Exception Handling UsingNoException TestMyExceptionWithTiming Run


Download ppt "Chapter 11: Exception Handling Exceptions and Exception Types Exceptions and Exception Types Claiming Exceptions Claiming Exceptions Throwing Exceptions."

Similar presentations


Ads by Google