Exceptions Review Checked Vs. Unchecked Exceptions Multiple Catch Blocks
Review Exception: An error caused by exceptional circumstance An object that is thrown when an exception occurs 9/3/2019 Wendi Jollymore, ACES
Review: try-catch Block 9/3/2019 Wendi Jollymore, ACES
Review: Declaring Exceptions Methods that don’t handle their own exceptions should declare them 9/3/2019 Wendi Jollymore, ACES
Review: Exercise Write a program that asks the user for a numeric value via Input Dialog Parse the number into an integer print the square of that number on the console Run the program and test it with a string value What exception occurs? Check the stack trace 9/3/2019 Wendi Jollymore, ACES
PROG 10082 - Object Oriented Programming 1 9/3/2019 Exceptions Hierarchy Internal System Errors: Not much you can do about them. 9/3/2019 Wendi Jollymore, ACES Wendi Jollymore, ACES
PROG 10082 - Object Oriented Programming 1 9/3/2019 Exceptions Hierarchy Execution errors you can catch and handle in your code 9/3/2019 Wendi Jollymore, ACES Wendi Jollymore, ACES
Exceptions Hierarchy A child exception object can be caught if parent is caught 9/3/2019 Wendi Jollymore, ACES
Checked vs. Unchecked Exceptions are either Checked or Unchecked You must handle them in some way You may not ignore them Unchecked Exceptions: They can go “unchecked” You’re not required to handle them 9/3/2019 Wendi Jollymore, ACES
Checked vs. Unchecked Checked Exceptions Example You may not compile a program with checked exceptions that are not caught or declared. 9/3/2019 Wendi Jollymore, ACES
Checked vs. Unchecked Unchecked Exception classes, examples: NumberFormatException InputMismatchException StringIndexOutOfBoundsException NullPointerException IllegalArgumentException 9/3/2019 Wendi Jollymore, ACES
Checked vs. Unchecked How do you know which is which? Unchecked 9/3/2019 Wendi Jollymore, ACES
Exceptions and the Call Stack Call Stack is the “breadcrumb trail” of calling methods in a program Exceptions can be thrown up the call stack to be handled by one main method See example: http://www-acad.sheridanc.on.ca/~jollymor/prog24178/oop2.html#methods 9/3/2019 Wendi Jollymore, ACES
Multiple Catch Blocks What if you have more than one kind of exception occurring and you desire different actions for each? A try catch can have more than one catch block One for each exception you want to handle Only one catch block will execute 9/3/2019 Wendi Jollymore, ACES
Multiple Catch Blocks 9/3/2019 Wendi Jollymore, ACES
Multiple Catch Blocks: Error! 9/3/2019 Wendi Jollymore, ACES
Exercise Design a program that calculates the difference between two times User enters a string in the form hh:mm:ss for two different amounts of time long getTotalSeconds(String time) Accepts one amount of time as a string and extracts hours, mins, seconds Calculates and returns the total amount of time (hours * 3600 + mins * 60 + seconds) Helper methods getHours(), getMinutes(), getSeconds() 9/3/2019 Wendi Jollymore, ACES
Exercise, continued To test, ask user for two time amounts Display difference in seconds What exceptions could occur in your methods? Should you put try-catch blocks in any method that might throw exceptions? Is there an easier way? Answer and code provided in class and in notes 9/3/2019 Wendi Jollymore, ACES