Download presentation
Presentation is loading. Please wait.
Published byBernard Anthony Modified over 8 years ago
1
Christina Aiello, 12/3/2015
2
Reminder: Seen in lecture notes for Exceptions: “Side note: we will NOT expect you to know/remember how to do keyboard input/output for either the programming exam or the final.”
3
Exceptions Occur when something goes wrong Better way of handling error checking than returning something like -1 Remember: Need to tell methods they can throw exceptions that they’re throwing Ex: If you tell the findByName method inside its method body to throw a CustNotFoundException, you have to change the method header to say: Customer findByName(String name) throws CustNotFoundException
4
Try, Catch, and Throw Analogy Think of “throw” as: You have a toaster that can detect when your bread is on fire. When it's burning, the toaster will start beeping. Think of throwing an exception as being the beeping of the toaster: When you detect something is wrong, alert the user of the toaster that something is wrong.
5
Try, Catch, and Throw Analogy Now, think of the try/catch as you actually using the toaster. You try to use the toaster. If something goes wrong (If an exception is thrown), you need to address the problem (catch it) by, for example, unplugging the toaster and getting the fire extinguisher. Why deal with the exception in the catch? Your toaster doesn't need to know what to do when it catches on fire other than beep (It can't go get a fire extinguisher because sadly the Brave Little Toaster movie isn’t real life), so at this point it's your turn to help!
6
Try, Catch, and Throw Analogy, All Together: You have a toaster that can detect when your bread is on fire. When your toast is burning, the toaster will start beeping, which is the toaster throwing an exception because something went wrong. One morning, you try to use the toaster. If something goes wrong (If an exception is thrown), you need to address the problem (catch it) by, for example, unplugging the toaster and getting the fire extinguisher. Your toaster doesn't need to know what to do when it catches on fire other than beep (It can't go get a fire extinguisher), so at this point it's your turn to help.
7
Compile-Time Errors These are errors detected by the compiler. Common causes include: Trying to access a variable that is not in scope (such as defining a variable inside an if statement and then trying to access it outside of that if statement) Syntax errors (such as missing semi-colon) When you declare multiple objects with the same name If the compiler detects any errors during compilation, it will fail to build a new assembly (You know those.class files? That’s what won’t be made).
8
Runtime Exception These occur when your code is actually running Classic example is IndexOutOfBoundsException A.k.a. off-by-one error Remember: Arrays and lists have zero-base indexes, meaning the first item in it is at index 0. Ex: Have an array or list of size 4. Try to access the item at index 4 in this array. This means there is no item at index 4 if the length is 4. The items are of indices 0, 1, 2, and 3.
9
Checked Exception vs Unchecked Exception Checked exceptions are checked at compile-time FileNotFoundException (program is told to look for a specific file) ClassNotFoundException (maybe some jar file you’re trying to use can’t be found, and it has a class in it that your code needs) Unchecked exceptions are checked at runtime Can occur due to: Network connectivity lost Ran out of memory on machine All unchecked exceptions are subclasses of RuntimeException class
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.