Christina Aiello, 12/3/2015
Handling errors in programming through exceptions is crucial for robust code. Learn about how exceptions occur, ways to handle them using try, catch, and throw, and the importance of proper error checking. Understand the analogy of exceptions to a toaster detecting a burning toast and how to address exceptions effectively.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
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.
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
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.
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!
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.
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).
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.
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