Chapter 13 Exception Handling F Exceptions and Exception
- Slides: 15
Chapter 13 Exception Handling F Exceptions and Exception Types F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions F The finally Clause F Cautions When Using Exceptions F Creating Your Own Exception Classes (Optional)
Exceptions and Exception Types
Claiming, Throwing, and Catching Exceptions
Claiming Exceptions F public void my. Method() throws IOException, Other. Exception
Throwing Exceptions F throw new The. Exception(); F The. Exception e = new The. Exception(); throw e;
Throwing Exceptions Example public Rational divide(Rational r) throws Exception { if (r. numer == 0) { throw new Exception("divisor cannot be zero"); } } long n = numer*r. denom; long d = denom*r. numer; return new Rational(n, d);
Catching Exceptions try { statements; } catch (Exception 1 e) { handler for exception 1 } catch (Exception 2 e) { handler for exception 2 }. . . catch (Exception. N e) { handler for exception. N }
Catching Exceptions
Example 13. 1 Claiming, Throwing, and Catching Exceptions F Objective: Write a program to test the new Rational class. Test. Rational. Exception Run Rational
Example 13. 2 Exceptions in GUI Applications F An error message appears on the console, but the GUI application continues running. F Re-run the Menu. Demo applet from Example 11. 9 and divide by 0 to see how a GUI deals with unhandled exceptions. Menu. Demo Run
Rethrowing Exceptions try { statements; } catch(The. Exception e) { perform operations before exits; throw e; }
The finally Clause try { statements; } catch(The. Exception e) { handling e; } finally { final. Statements; }
Cautions When Using Exceptions F Exception handling separates errorhandling code from normal programming tasks, thus making programs easier to read and to modify. Be aware, however, that exception handling usually requires more time and resources because it requires instantiating a new exception object, rolling back the call stack, and propagating the errors to the calling methods.
Example 13. 3 (Optional) Creating Your Own Exception Classes F Objective: This program creates a Java applet for handling account transactions. The applet displays the account id and balance, and lets the user deposit to or withdraw from the account. For each transaction, a message is displayed to indicate the status of the transaction: successful or failed. In case of failure, the failure reason is reported.
Example 13. 3, cont. Insufficient. Fund. Exception Account Negative. Amount. Exception Account. Applet Run
- Contoh error handling
- Error handling in vb
- Exception handling in java
- Apa yang dimaksud dengan exception handling
- Throw exception php
- Exception handling pada java
- No irq handler for vector
- Exception handling in ada
- Php custom exception
- Exception handling in vb net
- Thoughtful comparative and superlative
- Predefined and non predefined exceptions are raised
- Oldsuperlative
- Compararives
- Spl exceptions
- Robustness in java