Exceptions Review Checked Vs. Unchecked Exceptions

Slides:



Advertisements
Similar presentations
CMSC 202 Exceptions 2 nd Lecture. Aug 7, Methods may fail for multiple reasons public class BankAccount { private int balance = 0, minDeposit =
Advertisements

Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
COMP 121 Week 5: Exceptions and Exception Handling.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
Exception Handling Part 1: Handing Java Exceptions CSIS 3701: Advanced Object Oriented Programming.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Exceptions CIS 304 Intermediate Java Programming for Business.
1 Exception Handling (in a nutshell). 2 Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle the.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
Exceptions and Assertions Recitation – 03/13/2009 CS 180 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Java Software Solutions Foundations of Program Design Sixth Edition
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Preventing and Correcting Errors
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Java Programming: Guided Learning with Early Objects
Chapter 10 Exceptions. Chapter Scope The purpose of exceptions Exception messages The call stack trace The try-catch statement Exception propagation The.
COMP Exception Handling Yi Hong June 10, 2015.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Generics, Exception and Undo Command. A Generic Stack Class AStack uses generic. In the main method, we have 2 instances of AStack, each type is Integer.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.
Generics, Exceptions and Undo Command
OBJECT ORIENTED PROGRAMMING II LECTURE 10 GEORGE KOUTSOGIANNAKIS
Chapter 13 Exception Handling
Introduction Exception handling Exception Handles errors
CS102 – Exceptions David Davenport Latest: May 2015
Chapter 12 Exception Handling and Text IO
CSE 501N Fall ’09 17: Exception Handling
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Exception Handling Chapter 9.
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Abdulmotaleb El Saddik University of Ottawa
Exception Handling and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
Exception Handling.
Handling Exceptions.
Part B – Structured Exception Handling
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
Web Design & Development Lecture 7
Chapter 13 Exception Handling
CMSC 202 Exceptions 2nd Lecture.
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
Chapter 12 Exception Handling and Text IO Part 1
Chapter 11 Exceptions Java Software Solutions
Exceptions References: Jacquie Barker, Beginning Java Objects; Rick Mercer, Computing Fundamentals With Java; Wirfs-Brock et. al., Martin Fowler, OOPSLA.
Java Basics Exception Handling.
CMSC 202 Exceptions 2nd Lecture.
Presentation transcript:

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