Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 13 Exception.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions Chapter 18 Programs: DriverException2 DriverException TestAgeInputException.
Java Programming Exceptions. Java has a built in mechanism for error handling and trapping errors Usually this means dealing with abnormal events or code.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
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:
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
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.
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
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 1. Introduction Users may use our programs in an unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected.
Object Oriented Programming
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Slides Credit Umair Javed LUMS Web Application Development.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Java Programming: Guided Learning with Early Objects
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 15 Exceptions and.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
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.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
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.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
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 13 Exception Handling
Chapter 13 Exception Handling
Topic: Exception Handling
Introduction to Exceptions in Java
CS102 – Exceptions David Davenport Latest: May 2015
Chapter 12 Exception Handling and Text IO
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.
Chapter 12 Exception Handling
Chapter 13 Exception Handling
Exception Handling and Reading / Writing Files
Web Design & Development Lecture 7
Chapter 13 Exception Handling
Chapter 12 Exception Handling and Text IO Part 1
Exceptions References: Jacquie Barker, Beginning Java Objects; Rick Mercer, Computing Fundamentals With Java; Wirfs-Brock et. al., Martin Fowler, OOPSLA.
Java Basics Exception Handling.
Presentation transcript:

Lecture10 Exception Handling Jaeki Song

Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error The program is running if the environment detects an operation that is impossible to carry out –Logic error A program doesn’t perform the way it was intended to

Exception Runtime errors causes exceptions –Events that occur during the execution of a program and disrupt the normal flow of control A program that does not provide the code to handle exceptions may terminate abnormally, causing serious problem E.g. –The user may enter invalid input –The program may attempt to open file that doesn’t exist –The program may attempt to access an out-of-bound array element

Java and Exception LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException Several more classes

Java and Exception A java exception is an instance of a class derived from Throwable –Errors related to graphic are included in the java.awt package –Numeric exceptions are included in the java.lang package

Exception Classes The Exception class describes errors caused by your program and external circumstances –ClassNotFoundException If you attempt to use a class that does not exist –RuntimeException Describes programming errors, such as bad casting, accessing an out-of-bound array, and numeric errors

Common Exceptions ExceptionPurpose ArithmetricExceptionError caused by a calculation, such as division by zero NumberFormatException Problem converting a string to a number; occurs when the text field is blank or contains a fraction when an integer is required IllegalArgumentException Unable to format the value passed to one of the format methods FileNotFoundException File does not exist in path specified IOException Failure of an input or output operation such as reading from a file OutOfMemoryExceptionNot enough memory to create an object

Exception Handling The exception handling in Java consists of claiming exception, throwing exception, and catching and processing exceptions method1() { try { invoke method2; } catch (Exception ex) { Process exception; } } method2() throws Exception { if (an error occurs) { throw new Exception(); } } catch exception throw exception claim exception

Claiming Exceptions In general, every method states the types of exceptions it might encounter –Simply tells the compiler what might go wrong when the method is executing To claim an exception in a method, you use the throws keyword public void myMethod( ) throws IOException public void myMethod( ) throws IOException, OtherExceptions

Throwing Exceptions When a statement causes errors, the method containing the statement creates an exception object and passes it to the system –The exception object contains information about exception, including its type when the error occurred In the method that has claimed the exception, the following is the syntax to throw an exception: TheException ex = new TheException( ); throw ex

Catching Exceptions After a method throws an exception, the Java runtime systems begins the process of finding the code to handle the error –The code that handles the error is called the exception handler –The handler must match the type of exception thrown If no handler is found, the program terminates

Catching Exceptions When calling a method that explicitly claims an exception, you must use the try-catch block try { statement; } //statements that may throw exceptions catch (Exception1 ex) { handler for exception1;} catch (Exception2 ex) {handler for exception2;}

Catching Exceptions If no exception arise during the execution of the try clause, the catch clauses are skipped If one of the statement inside the try block throws an exception, Java skips the remaining statements and starts to search for a handler for the exception

Catching Exceptions main method {... try {... invoke method1; statement1; } catch (Exception1 ex1) { Process ex1; } } method1 {... try {... invoke method2; statement2; } catch (Exception2 ex2) { Process ex2; } } method2 {... try {... invoke method3; statement3; } catch (Exception3 ex3) { Process ex3; } }

Example Example 1 Example 2

The Exception Object If an exception of a subclass of Exception occurs in a graphics program, Java prints the error message –The exception object contains valuable information about the exception

The Exception Object public String getMessage() –Returns the detailed message of the Throwable object public String toString() –Returns a short description of the Throwable object public void printStackTrace( ) –Prints the Throwable object and its trace information

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

Cautions When Using Exceptions Exception handling separates error-handling code from normal programming tasks, thus making programs easier to read and to modify. Be aware, however, that exception handling usually requires more time and resources because it requires instantiating a new exception object, rolling back the call stack, and propagating the errors to the calling methods.

Customized Exception Handling Procedures –Step 1: Create own exception class by extending Exception and creating its own constructors Your exception class can inherit from the Exception class public class NegativeAmountException extends Exception { public NegativeAmountException() { super (“Negative amount “); statement; }

Customized Exception Handling Step2: Throws exception when certain conditions happens public void deposit (double amount) throws NegativeException { statement; }

Customized Exception Handling Step3: Provide the try and catch clauses for the thrown exception try { statement; } catch { statement; }