Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Similar documents
COE318 Lecture Notes Week 10 (Nov 7, 2011)

Programming II (CS300)

Programming II (CS300)

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

More on Exception Handling

Chapter 5. Exceptions. CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science. Dr. S.

CS112 Lecture: Exceptions. Objectives: 1. Introduce the concepts of program robustness and reliability 2. Introduce exceptions

Exception-Handling Overview

More on Exception Handling

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

C16b: Exception Handling

Exceptions and Error Handling

Exceptions. CSC207 Winter 2017

ITI Introduction to Computing II

BBM 102 Introduction to Programming II Spring Exceptions

ITI Introduction to Computing II

Introduction to Computer Science II CS S-22 Exceptions

CS112 Lecture: Exceptions and Assertions

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

Full file at Chapter 2 - Inheritance and Exception Handling

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Fundamentals of Object Oriented Programming

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

CS 151. Exceptions & Javadoc. slides available on course website. Sunday, September 9, 12

EXCEPTIONS. Java Programming

Exception in thread "main" java.lang.arithmeticexception: / by zero at DefaultExceptionHandling.main(DefaultExceptionHandling.

CSCI 261 Computer Science II

COMP1008 Exceptions. Runtime Error

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

16-Dec-10. Consider the following method:

CS S-22 Exceptions 1. Running a web server, don t want one piece of bad data to bring the whole thing down

Chapter 12 Exception Handling

ECE 122. Engineering Problem Solving with Java

Introduction. Exceptions: An OO Way for Handling Errors. Common Runtime Errors. Error Handling. Without Error Handling Example 1

CS159. Nathan Sprague

About This Lecture. Outline. Handling Unusual Situations. Reacting to errors. Exceptions

Exceptions - Example. Exceptions - Example

Exceptions and Libraries

BBM 102 Introduction to Programming II Spring 2017

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

CS 209 Programming in Java #10 Exception Handling

CS159. Nathan Sprague

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Contents. I Introductory Examples. Topic 06 -Exception Handling

COMP-202 Unit 9: Exceptions

EXCEPTIONS. Objectives. The try and catch Statements. Define exceptions. Use try, catch and finally statements. Describe exception categories

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

CS1020 Data Structures and Algorithms I Lecture Note #8. Exceptions Handling exceptional events

Exceptions, try - catch - finally, throws keyword. JAVA Standard Edition

CSE 143 Java. Exceptions 1/25/

Chapter 9. Exception Handling. Copyright 2016 Pearson Inc. All rights reserved.

Defensive Programming

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

Administrivia. CPSC Winter 2008 Term 1. Department of Computer Science Undergraduate Events

Chapter 13 Exception Handling

Input-Output and Exception Handling

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Exceptions Handling Errors using Exceptions

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

Day 8. COMP1006/1406 Summer M. Jason Hinek Carleton University

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan

COMP-202 Unit 9: Exceptions

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Why Exceptions? (1.1) Exceptions. Why Exceptions? (1.2) Caller vs. Callee. EECS2030 B: Advanced Object Oriented Programming Fall 2018

Exceptions. EECS2030 B: Advanced Object Oriented Programming Fall 2018 CHEN-WEI WANG

Exceptions. What exceptional things might our programs run in to?

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

Chapter 15. Exception Handling. Chapter Goals. Error Handling. Error Handling. Throwing Exceptions. Throwing Exceptions

COSC 123 Computer Creativity. I/O Streams and Exceptions. Dr. Ramon Lawrence University of British Columbia Okanagan

Introduction Unit 4: Input, output and exceptions

CSC Java Programming, Fall Java Data Types and Control Constructs

Preconditions. CMSC 330: Organization of Programming Languages. Signaling Errors. Dealing with Errors

Chapter 12: Exceptions and Advanced File I/O

Lecture 19 Programming Exceptions CSE11 Fall 2013

Exceptions. Examples of code which shows the syntax and all that

What are Exceptions?

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

CMSC131. Exceptions and Exception Handling. When things go "wrong" in a program, what should happen.

EXCEPTION HANDLING. Summer 2018

17. Handling Runtime Problems

File I/O and Exceptions

Programming Languages and Techniques (CIS120)

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan

CS 11 java track: lecture 3

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors and exceptions

To Think About. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

Software Practice 1 - Error Handling

Java Exception. Wang Yang

15CS45 : OBJECT ORIENTED CONCEPTS

EXCEPTION HANDLING. // code that may throw an exception } catch (ExceptionType parametername) {

Chapter 11 Handling Exceptions and Events. Chapter Objectives

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

Transcription:

Chapter 8 Exception Handling CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Clarifications Auto cast from char to String does not happen. Cast between int and char happens automatically. Class is a data type. It is possible to get the class of any object. Part of Reflection in Java Advanced topic 2

When things go wrong Good programs should be robust -- I.e., they should be able to handle exceptional situations. What happens if we are trying to input an integer value and the user enters ten, or 3.45? A good program should tell the user to re-enter a valid integer. So far, a situation such as this would result in the termination of our program when we execute Integer.parseInt() on this invalid string. How do we prevent this? 3

Handling errors One idea is to use if -then style tests whenever we expect that an error may arise. This is the style in C -- return values can signal the existence of an error. But this is clumsy, and inelegant. In Java, the exception handling mechanism is used instead. Unexpected (or unusual) cases are handled by a special type of control flow. 4

Exceptions An exception is used to indicate that something unusual (that prevents regular processing) has occurred. When an exception occurs, or is thrown, an Exception object is created, and the normal sequence of flow is terminated. An exception handling mechanism is invoked which is responsible for handling or catching the thrown exception. 5

Uncaught Exceptions When an exception is thrown, and the program does not specify how to catch it, it causes the program to terminate: import javax.swing.*; public class Test{ public static void main(string[] args){ String inputstr; int i; inputstr = JOptionPane.showInputDialog(null, Enter Deposit Amount ); i = Integer.parseInt(inputStr); 6

Catching an exception String inputstr; int i; inputstr = JOptionPane.showInputDialog(null, Enter an integer ); try{ i = Integer.parseInt(inputStr); catch (Exception e){ System.out.println( Invalid integer ); 7

Exception control-flow No exception Exception thrown try{ stmt; catch (Exception e){ try{ stmt; catch (Exception e){ Exception is thrown when executing this 8 statement.

Exception object An exception is thrown by creating an Exception object. The exception object is passed to the catch block as a parameter. It contains details about the actual exception that was thrown. try { catch (Exception e){ e is a catch block parameter corresponding to the exception object. 9

Exception object The exception object contains details about the exception. The getmessage() method simply returns a string of text that describes the exception. The printstacktrace() method gives us the order (and line numbers) in which methods had been called when the exception took place. In reverse order of the calls The last method call is listed first, main is last. 10

The Exception Hierarchy Exception IOException RunTimeException SQLException NullPointerException IllegalArgumentException ArithmeticException NumberFormatException Many more. See Java API 11

Multiple catch Blocks If more than one type of exception can take place, we may want to handle each one differently. A single try-catch statement can include multiple catch blocks, one for each type of exception. Only the first matching catch block is executed. Matching is based on the class of the exception. Make sure to list classes lower in the hierarchy before listing classes higher up. 12

Multiple catch Blocks Student s; try { i = Integer.parseInt(inputStr); s.getname(); catch (NumberFormatException e){ // code to handle NumberFormatExceptions. catch (NullPointerException e){ // code to handle NullPointerExceptions. catch (Exception e){ // code to handle all other exceptions. 13

Terminating a program It is possible to terminate a program at any point in its execution (maybe because a very serious error has occurred). This is achieved by calling System.exit(0) This call takes any integer value as a parameter. The program is immediately terminated. 14

The finally Block There are situations where we need to take certain actions regardless of whether an exception is thrown or not. We place statements that must be executed regardless of exceptions, in the finally block. Commonly used to perform cleanup (e.g., closing disconnecting from a database, or closing a network connection) 15

Exception control-flow No exception finally block is always executed. Exception thrown try{ stmt; catch (Exception e){ finally { try{ stmt; catch (Exception e){ finally { Exception is thrown when executing this 16 statement.

Salient points If multiple catch blocks are defined they are tested in order -- only the first that matches the thrown exception gets executed. List them from more specific to general. CAUTION: if A is a subclass of B, then an exception of class A is also an exception of class B! Even if there is a return from the try or catch blocks, the finally block is executed before returning! If no matching catch block is found for an exception, the finally block gets executed 17

Caution: order of catch blocks Student s; try { i = Integer.parseInt(inputStr); s.getname(); catch (Exception e){ // code to handle general exceptions. catch (NullPointerException e){ // code to handle NullPointerExceptions. catch (NumberFormatException e){ Will never get executed! // code to handle NumberFormatExceptions. 18

Propagating exceptions If an exception occurs and there is no matching catch block, then the exception is propagated. control passes to the caller if the caller has no matching catch block, the same happens eventually, if the main method does not handle the exception, the runtime system handles it. 19

Exception handling public static void main(string[] args){... a.methoda();... Exception is handled. public void methoda(){ try{ methodb(); catch (NumberFormatException e){ public void methodb(){ stmt; NumberFormatException is thrown when executing this statement. Exception is propagated 20

Exception handling public static void main(string[] args){... a.methoda();... Program is terminated. Exception is propagated. public void methoda(){ try{ methodb(); catch (NumberFormatException e){ public void methodb(){ stmt; NullPointerException is thrown when executing this statement. Exception is propagated 21

Types of exceptions Two main types of exceptions Checked exceptions Unchecked exceptions. Unchecked exceptions are those that can be thrown during the normal operation of the Java Virtual Machine Captured under the RuntimeException class in the hierarchy. NullPointerException, ArithmeticException, IndexOutOfBoundsException, etc. 22

Types of exceptions (cont.) Unchecked exceptions need not be explicitly handled (as we have done so far) If unhandled, will lead to program termination. Checked exceptions must be explicitly handled by the program. Any method that could result in a checked exception being thrown must either: Handle it with a try-catch block, OR Propagate and explicitly declare this possibility. 23

Propagating Checked Exceptions A method that propagates an unchecked exception must declare this possibility: the method header must include the reserved word throws followed by a list of the classes of exceptions that may be propagated declaring runtime exceptions is optional public int accessdb( ) throws SQLException { // code that accesses some database 24

Handling Unchecked Exceptions parseint throws NumberFormatException (see API). void methoda( ){ try { int i = Integer.parseInt(s); catch (NumberFormatException e) { Catcher Propagators void methodb( ) { int i = Integer.parseInt(s); void methodb( ) throws NumberFormatException { int i = Integer.parseInt(s); 25

Handling Checked Exceptions Scanner(File ) throws FileNotFoundException (see API). void methoda( ){ try { scanner = new Scanner(f); catch (FileNotFoundException e) { Catcher Propagator void methodb( ) throws FileNotFoundException { scanner = new Scanner(f); 26

Throwing Exceptions We can throw an exception at any point in our code. To do this, we create an exception object and throw it. If this is a checked exception, we must declare it (unless we catch the exception). public float squareroot(float value) throws Exception { if (value<0) throw new Exception ("Imaginary numbers not yet supported"); 27

Quiz Which of the following is true? A. throws is used to create an exception B. throw is used to indicate that a method may propagate an exception C.Only one catch block can be declared for a given try block. D.The finally block can be skipped by returning from a try block. 28

Defining your own exceptions Should only need to do this if we want to capture extra information, or if you want to handle this class in a special fashion. Must extend an exception class. Good idea to extend the Exception class. Define a default constructor. Call the parent s constructor as the first call in the constructor for the new exception: super(msg); 29

Ascending Input Helper Let us assume that our application often needs to input several streams in integers in ascending order with a minimum jump between values. each stream has its own starting point and minimum jump. Create a helper class to input such values: AscendingInputHelper This class throws a new type of exception that signals that the ascending rule was violated: AscendingException. 30

AscendingInputHelper class AscendingInputHelper { int lastvalue; // the previous input for this sequence int minimumincrement; // the minimum increment required public AscendingInputHelper(int start, int mininc){ lastvalue = start; minimumincrement = mininc; // Propagate exception public int getnextint() throws AscendingException { int i; i = SafeInputHelper3.getInt(); //Get the next integer from the user if(i< lastvalue+minimumincrement) //if invalid ascend, throw exception throw new AscendingException(i, lastvalue, minimumincrement); lastvalue=i; return i; 31

AscendingException public class AscendingException extends Exception { private int lastentry, errorentry, minimumincrement; private static final String ERROR_MSG = "Invalid Ascending Sequence"; public AscendingException(int badentry, int last, int inc){ this(ascendingexception.error_msg, badentry, last, inc); public AscendingException (String msg, int badentry, int last, int inc){ super(msg); errorentry = badentry; lastentry = lastentry; minimumincrement = inc; public int getlastentry(){return lastentry; public int geterrorentry(){return errorentry; public int getminimumincrement(){return minimumincrement; 32

Using AscendingInputHelper class TestAscendingInput { public static void main(string args[]) { int[] inputvalues = new int[5]; boolean done; AscendingInputHelper ascinput = new AscendingInputHelper(0, 5); for(int i=0; i<inputvalues.length; i++){ done=false; while(!done) { try { inputvalues[i] = ascinput.getnextint(); JOptionPane.showMessageDialog(null,"Received input: " + inputvalues[i]); done = true; catch (AscendingException e) { JOptionPane.showMessageDialog(null, "Error with order of input\n" + e.getmessage() + "\nentered value: " + e.geterrorentry() + "\n Previous value: " + e.getlastentry() + "\n Mininum Increment required: " + e.getminimumincrement()); 33

Javadoc 34

Javadoc Comments Comments are essential for documenting a piece of code. Javadoc is a very useful tool for automatically (almost) creating HTML-based files for documenting our classes. This result is similar to the Java API. also done using javadoc! To generate Javadoc documentation: add comments that follow the javadoc style; use %javadoc *.java to generate the html. 35

javadoc Tags Javadoc comments begins with /** and end with */ Special information is annotated in the comments: @param @author @return @throws @see (see http://java.sun.com/j2se/javadoc) 36