Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exceptions Review Checked Vs. Unchecked Exceptions

Similar presentations


Presentation on theme: "Exceptions Review Checked Vs. Unchecked Exceptions"— Presentation transcript:

1 Exceptions Review Checked Vs. Unchecked Exceptions Multiple Catch Blocks

2 Review Exception: An error caused by exceptional circumstance
An object that is thrown when an exception occurs 9/3/2019 Wendi Jollymore, ACES

3 Review: try-catch Block
9/3/2019 Wendi Jollymore, ACES

4 Review: Declaring Exceptions
Methods that don’t handle their own exceptions should declare them 9/3/2019 Wendi Jollymore, ACES

5 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

6 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

7 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

8 Exceptions Hierarchy A child exception object can be caught if parent is caught 9/3/2019 Wendi Jollymore, ACES

9 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

10 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

11 Checked vs. Unchecked Unchecked Exception classes, examples:
NumberFormatException InputMismatchException StringIndexOutOfBoundsException NullPointerException IllegalArgumentException 9/3/2019 Wendi Jollymore, ACES

12 Checked vs. Unchecked How do you know which is which? Unchecked
9/3/2019 Wendi Jollymore, ACES

13 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: 9/3/2019 Wendi Jollymore, ACES

14 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

15 Multiple Catch Blocks 9/3/2019 Wendi Jollymore, ACES

16 Multiple Catch Blocks: Error!
9/3/2019 Wendi Jollymore, ACES

17 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 * mins * 60 + seconds) Helper methods getHours(), getMinutes(), getSeconds() 9/3/2019 Wendi Jollymore, ACES

18 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


Download ppt "Exceptions Review Checked Vs. Unchecked Exceptions"

Similar presentations


Ads by Google