Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this...
-
Upload
francine-underwood -
Category
Documents
-
view
215 -
download
0
Transcript of Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this...
![Page 1: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/1.jpg)
Question of the Day
A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?
![Page 2: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/2.jpg)
Question of the Day
A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?
![Page 3: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/3.jpg)
Question of the Day
A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?
![Page 4: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/4.jpg)
LECTURE 14:EXCEPTIONS
![Page 5: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/5.jpg)
Error Handling Goals
![Page 6: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/6.jpg)
Error Handling Goals
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
![Page 7: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/7.jpg)
Exceptional Circumstances
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
![Page 8: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/8.jpg)
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
![Page 9: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/9.jpg)
Error Handling Codes
throw exception upon detecting problem
Handle problem by catching 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
![Page 10: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/10.jpg)
Throwing an Exception
public class BankAccount { private float balance;
// Lots of code here…
float withdraw(float amt) throws ReqException{ if (amt < 0) { throw new ReqException(); } else if (amt > balance) { ReqException re = new ReqException(); re.setBadRequest(amt, balance); throw re; } balance -= amt; return balance; }}
![Page 11: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/11.jpg)
Handling Exceptions
Once an exception is thrown, methods can: Include code to catch exception and then
ignore it
![Page 12: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/12.jpg)
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
![Page 13: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/13.jpg)
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
![Page 14: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/14.jpg)
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
![Page 15: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/15.jpg)
Handling Exceptions
Exception can be thrown anywhere & anytime 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 otherwisevoid plan(int i) throws NoFreeTime { // Lots of interesting code here… scheduler();}
![Page 16: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/16.jpg)
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();
}
![Page 17: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/17.jpg)
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();
}
![Page 18: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/18.jpg)
try {…} Blocks
Can catch some (or all) exceptions from try 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!”);}
}
![Page 19: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/19.jpg)
try {…} Blocks
If exception thrown, stop try & go to catchvoid 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.”);}
}
![Page 20: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/20.jpg)
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);
}
![Page 21: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/21.jpg)
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
![Page 22: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/22.jpg)
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();}
}
![Page 23: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/23.jpg)
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();}
}
![Page 24: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/24.jpg)
2 Types of Exceptions
Checked Exception Unchecked Exception
![Page 25: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/25.jpg)
2 Types of Exceptions
Subclass of Exception 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 Exception Unchecked Exception
![Page 26: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/26.jpg)
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);
}
![Page 27: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/27.jpg)
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);
}
![Page 28: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/28.jpg)
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);
}
![Page 29: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/29.jpg)
Your Turn
Get into your groups and complete activity
![Page 30: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/30.jpg)
For Midterm
You can use on this midterm: You can always use your textbook & notes Printout of slides IF has notes on that day's
slides At the same time, you may NOT use:
Computer, calculator, cell phone, or similar Copies of daily activities and/or solutions Friends, Romans, countrymen or their ears
To be certain rules are followed, when test ends Hand in all printed material you had with
you
![Page 31: Question of the Day A landscaper plants 5 rows of 4 trees each, but only uses 10 trees. How is this possible?](https://reader030.fdocuments.net/reader030/viewer/2022033108/56649efc5503460f94c10295/html5/thumbnails/31.jpg)
How to Prepare for Midterm
DO DON'T
Make cheat sheets for the test
Review how parts of Java work
Add post-its to important pages
Memorize Drink case of 40s before
test Use post-its as clothing