Chapter 13 Exception Handling

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 13 Exception.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
CS203 Java Object Oriented Programming Errors and Exception Handling.
Java Software Solutions Foundations of Program Design Sixth Edition
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 14 Exception Handling and Text.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 12 Exception Handling and Text.
1 Chapter 18 Exception Handling. 2 Motivations F Program runs into a runtime error –program terminates abnormally F How can you handle the runtime error.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Chapter 11 Exception Handling F Exceptions and Exception Types F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Exception Handling.
Exception Handling. Definition  The term exception is shorthand for the phrase "exceptional event.“  An exception is an event, which occurs during the.
CIS 270—Application Development II Chapter 13—Exception Handling.
Programming in C++ 1. Learning Outcome At the end of this slide, student able to:  Identify the different types of error in your program  Understand.
Slides Credit Umair Javed LUMS Web Application Development.
COMP Exception Handling Yi Hong June 10, 2015.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 15 Exceptions and.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Exception Handling and Text.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Exceptions and Assertions Chapter 15 – CSCI 1302.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
1 Chapter 15 Exceptions and Assertions. 2 Objectives F To know what is exception and what is exception handling (§15.2). F To distinguish exception types:
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 15 Exceptions and Assertions Nothing is impossible.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
CS 112 Programming 2 Lecture 08 Exception Handling & Text I/O (1)
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Chapter 12 Exceptions and File Input/Output
Java Exceptions a quick review….
Chapter 13 Exception Handling
Chapter 12 Exception Handling And Text IO
Chapter 13 Exception Handling
Exception Handling in Java Reference: COS240 Syllabus
Topic: Exception Handling
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling & Text Files
Chapter 7 Exceptions and Assertions
Chapter 12 Exception Handling and Text IO
Chapter 14 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling
Chapter 13 Exception Handling
Chapter 11 Exception Handling and Text I/O
Exception Handling and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Web Design & Development Lecture 7
Chapter 12 Exception Handling and Text IO
Chapter 14 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Chapter 14 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO Part 1
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Java Basics Exception Handling.
Exception Handling.
Exceptions and Exception Handling
Presentation transcript:

Chapter 13 Exception Handling

Exception-Handling Overview Show runtime error Quotient Run Fix it using an if statement QuotientWithIf Run What if the runtime error occurs in a called method? QuotientWithException Run

Handling InputMismatchException InputMismatchExceptionDemo Run By handling InputMismatchException, your program will continuously read an input until it is correct.

Exception Types

System Errors System errors are thrown by JVM and represented in the Error class. The Error class describes internal system errors. Such errors rarely occur. If one does, there is little you can do beyond notifying the user and trying to terminate the program gracefully.

Exceptions Exception describes errors caused by your program and external circumstances. These errors can be caught and handled by your program.

Runtime Exceptions RuntimeException is caused by programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors.

Checked Exceptions vs. Unchecked Exceptions RuntimeException, Error and their subclasses are known as unchecked exceptions. All other exceptions are known as checked exceptions, meaning that the compiler forces the programmer to check and deal with the exceptions.

Unchecked Exceptions In most cases, unchecked exceptions reflect programming logic errors that are not recoverable. For example, an IndexOutOfBoundsException is thrown if you access an element in an array outside the bounds of the array. These are the logic errors that should be corrected in the program. Unchecked exceptions can occur anywhere in the program. To avoid cumbersome overuse of try-catch blocks, Java does not mandate you to write code to catch unchecked exceptions.

Unchecked Exceptions Unchecked exception.

Declaring Exceptions Every method must state the types of checked exceptions it might throw. This is known as declaring exceptions. public void myMethod() throws IOException throws IOException, OtherException

Throwing Exceptions When the program detects an error, the program can create an instance of an appropriate exception type and throw it. This is known as throwing an exception. Here is an example, throw new TheException(); TheException ex = new TheException(); throw ex;

Throwing Exceptions Example /** Set a new radius */ public void setRadius(double newRadius)throws IllegalArgumentException{ if (newRadius >= 0) radius = newRadius; else throw new IllegalArgumentException("Radius cannot be negative"); }

Catching Exceptions try { statements; // Statements that may throw exceptions } catch (Exception1 exVar1) { handler for exception1; catch (Exception2 exVar2) { handler for exception2; ... catch (ExceptionN exVar3) { handler for exceptionN;

Catch or Declare Checked Exceptions Java forces you to deal with checked exceptions. If a method declares a checked exception (i.e., an exception other than Error or RuntimeException), you must invoke it in a try-catch block or declare to throw the exception in the calling method. For example, suppose that method p1 invokes method p2 and p2 may throw a checked exception (e.g., IOException), you have to write the code as shown in (a) or (b).

Example: Declaring, Throwing, and Catching Exceptions Objective: This example demonstrates declaring, throwing, and catching exceptions by modifying the setRadius method in the Circle class defined in Chapter 8. The new setRadius method throws an exception if radius is negative. TestCircleWithException CircleWithException Run

The finally Clause try { statements; } catch(TheException ex) { handling ex; finally { finalStatements;

Trace a Program Execution animation Trace a Program Execution Suppose no exceptions in the statements try { statements; } catch(TheException ex) { handling ex; finally { finalStatements; Next statement;

Trace a Program Execution animation Trace a Program Execution The final block is always executed try { statements; } catch(TheException ex) { handling ex; finally { finalStatements; Next statement;

Trace a Program Execution animation Trace a Program Execution Next statement in the method is executed try { statements; } catch(TheException ex) { handling ex; finally { finalStatements; Next statement;

Trace a Program Execution animation Trace a Program Execution Suppose an exception of type Exception1 is thrown in statement2 try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; finally { finalStatements; Next statement;

Trace a Program Execution animation Trace a Program Execution The exception is handled. try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; finally { finalStatements; Next statement;

Trace a Program Execution animation Trace a Program Execution The final block is always executed. try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; finally { finalStatements; Next statement;

Trace a Program Execution animation Trace a Program Execution The next statement in the method is now executed. try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; finally { finalStatements; Next statement;

Trace a Program Execution animation Trace a Program Execution try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; catch(Exception2 ex) { throw ex; finally { finalStatements; Next statement; statement2 throws an exception of type Exception2.

Trace a Program Execution animation Trace a Program Execution try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; catch(Exception2 ex) { throw ex; finally { finalStatements; Next statement; Handling exception

Trace a Program Execution animation Trace a Program Execution try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; catch(Exception2 ex) { throw ex; finally { finalStatements; Next statement; Execute the final block

Trace a Program Execution animation Trace a Program Execution try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; catch(Exception2 ex) { throw ex; finally { finalStatements; Next statement; Rethrow the exception and control is transferred to the caller

When to Throw Exceptions An exception occurs in a method. If you want the exception to be processed by its caller, you should create an exception object and throw it. If you can handle the exception in the method where it occurs, there is no need to throw it.