Download presentation
Presentation is loading. Please wait.
Published bySharleen Underwood Modified over 9 years ago
1
Question of the Day completes starts What word completes the 1 st word & starts the 2 nd one: DON???CAR
2
Question of the Day completes starts What word completes the 1 st word & starts the 2 nd one: DON???CAR Key key Answer: Key It completes the 1 st word (don KEY ) & a key starts a car
4
Error Handling Goals
5
What should we do when an error occurs? Should alert system to the error Stop immediately and execute code handling error Minimize amount of rewritten code How error handled & fixed may change in subclass Unknown future uses of code demands flexibility Changing printing of error message is not possible
6
Exceptional Circumstances ex-cep-tion ex-cep-tion : n. Situation or case not conforming to the general rule Proper way to signal error in object-oriented code Seen Java exceptions in CSC111 already Most of these were unchecked (not solvable) ArrayIndexOutOfBoundsException -- accessing a nonexistent array entry NullPointerException -- used null reference
7
Exception Classes Java Exception is class defined by Java Java already defines many subclasses of Exception New Exception subclasses can be written, also Error handling uses instances of these classes Classes are not magic and work like all others Classes can include fields, methods, & constructors Exception instances not special, either Must instantiate before use Use anywhere, not strictly for error handling
8
Error Handling Codes throw exception upon detecting problem Handle problem by catch ing exception Do not need to catch an exceptions If it is never caught, program will crash Not a bad thing if the error was unfixable, however
9
Throwing an Exception public class BankAccount { private float balance; // Lots of code here… float withdraw(float amt) throws ReqException{ if (amt balance) { ReqException re = new ReqException(); re.setBadRequest(amt, balance); throw re; } balance -= amt; return balance; } }
10
Handling Exceptions Once an exception is thrown, methods can: Include code to catch exception and then ignore it
11
Handling Exceptions Once an exception is thrown, methods can: Include code to catch exception and then ignore it Catch & handle exception so error is fixed
12
Handling Exceptions Once an exception is thrown, methods can: Include code to catch exception and then ignore it Catch & handle exception so error is fixed Catch the exception and throw a new one
13
Handling Exceptions Once an exception is thrown, methods can: Include code to catch exception and then ignore it Catch & handle exception so error is fixed Catch the exception and throw a new one Ignore exception so passed on to calling method
14
Handling Exceptions Exception can be thrown anywhere & anytime uncaught throws lists method’s fixable uncaught exceptions void controller() throws LostPlane {…} int scheduler() throws NoFreeTime {…} Calling methods now aware of possible errors If it would like, could catch and correct errors List exception in own throws clause otherwise void plan(int i) throws NoFreeTime { // Lots of interesting code here… scheduler(); }
15
try {…} Blocks Can only catch exceptions thrown in try block void cantCatch() throws MistakeExcept,MyBadExcept{ try { // There is code here that does something interesting… System.err.println(“No exceptions”); } catch (Oops oop) { // Here be code to fix the exception } methodThatMightThrowMistakeExcept(); throw new MyBadExcept(); }
16
try {…} Blocks
17
Can catch some (or all) exceptions from try at least 1 Each try needs at least 1 catch void catchSome() throws MyBadException { try { methodThatMightThrowExceptions(); throw new MyBadExcept(); } catch (Oops oop) { oop.printStackTrace(); System.err.println(“Method ends normally.”); } catch (MistakeException me) { System.err.println(“Boy was I dumb!”); } }
18
try {…} Blocks If exception thrown, stop try & go to catch void catchAll() { try { methodThatMightThrowOops(); methodThatShouldThrowOops(); throw new MyBadExcept(); } catch (Oops oop) { oop.printStackTrace(); System.err.println(“Oops was handled.”); } catch (MyBadExcept mbe) { mbe.printStackTrace(); System.err.println(“MyBad fixed.”); } }
19
Not Handling Exceptions float withdraw(float amt) throws BadReqEx { if (amt > balance) { BadReqEx re = new BadReqEx(amt,balance); throw re; } balance -= amt; return balance; } void forcedWithdrawal(float amount) { callPolice(); addDyePacks(); withdrawal(amount); } public void robbedByJesseJames(float amt) { forcedWithdrawal(amt); }
20
Not Handling Exceptions float withdraw(float amt) throws BadReqEx { if (amt > balance) { BadReqEx re = new BadReqEx(amt,balance); throw re; } balance -= amt; return balance; } void forcedWithdrawal(float amount) { callPolice(); addDyePacks(); withdrawal(amount); } public void robbedByJesseJames(float amt) { forcedWithdrawal(amt); } Unfair - by not stating exception, caller hasn't chance to handle
21
Handling Exceptions void forcedWithdrawal(float amount) throws BadReqEx{ callPolice(); addDyePacks(); withdrawal(amount); } public void northfieldMN(float amt) { try { forcedWithdrawal(amt); } catch (BadReqEx bre) { formPosse(); killSomeGangMembers(); } finally { giveLollipop(); } }
22
Handling Exceptions void forcedWithdrawal(float amount) throws BadReqEx{ callPolice(); addDyePacks(); withdrawal(amount); } public void northfieldMN(float amt) { try { forcedWithdrawal(amt); } catch (BadReqEx bre) { formPosse(); killSomeGangMembers(); } finally { giveLollipop(); } }
23
2 Types of Exceptions Checked ExceptionUnchecked Exception
24
2 Types of Exceptions Subclass of Exception must throws must list uncaught Use for fixable errors Java forces methods to consider them Only useful if fixable Subclass of RuntimeException Can be listed in throws “You are hosed” Usually can’t be fixed Can ignore in method Unless it is caught, these will still crash program Checked ExceptionUnchecked Exception
25
Tracing Example public static void generate() throws TraceException { TraceException te = new TraceException(); throw te; System.out.println(“Ending gE”); } public static void handler(boolean callIt) { try { System.out.println(“Starting cE”); if (callIt) { generate(); } System.out.println(“Ending cE”); } catch (TraceException te) { System.out.println(“Caught te”); } } public static void main(String[] args) { handler(false); handler(true); }
26
Tracing Example public static void generate() throws TraceException { TraceException te = new TraceException(); throw te; System.out.println(“Ending gE”); } public static void handler(boolean callIt) { try { System.out.println(“Starting cE”); if (callIt) { generate(); } System.out.println(“Ending cE”); } catch (TraceException te) { System.out.println(“Caught te”); } } public static void main(String[] args) { handler(false); handler(true); }
27
Tracing Example public static void generate() throws TraceException { TraceException te = new TraceException(); throw te; } public static void handler(boolean callIt) { try { System.out.println(“Starting cE”); if (callIt) { generate(); } System.out.println(“Ending cE”); } catch (TraceException te) { System.out.println(“Caught te”); } } public static void main(String[] args) { handler(false); handler(true); }
28
Your Turn Get into your groups and complete activity
29
For Next Lecture Read GT4.1-4.3 for class on Friday Is all code really equal? How can we figure out which is faster? Week #6 weekly assignment due Wednesday Note the change in date since no class next Tuesday Only 2 problems this week, since no class on Monday NO!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.