What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Exception Handling. Background In a perfect world, users would never enter data in the wrong form, files they choose to open would always exist, and code.
Exception Handling. Introduction Errors can be dealt with at place error occurs –Easy to see if proper error checking implemented –Harder to read application.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
CS102--Object Oriented Programming
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.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
Exception Handling Chapter 8. Outline Basic Exception Handling Defining Exception Classes Using Exception Classes.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
Java Programming Exceptions. Java has a built in mechanism for error handling and trapping errors Usually this means dealing with abnormal events or code.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
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.
Objectives Understanding what an exception is Understanding the heirarchy of exception classes Learn the types of exception and how to catch and handle.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
12.1 Exceptions The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make.
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
Preventing and Correcting Errors
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
Chapter 12: Exception Handling
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
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
COMP Exception Handling Yi Hong June 10, 2015.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
1 Exception handling in Java Reading for this lecture: Weiss, Section 2.5 (exception handling), p. 47. ProgramLive, chapter 10. I need to know whether.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
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.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
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.
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.
Introduction to Exceptions in Java CS201, SW Development Methods.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Java Exceptions a quick review….
Chapter 10 – Exception Handling
Exceptions 10-Nov-18.
Exception Handling Chapter 9 Edited by JJ.
Web Design & Development Lecture 7
Exceptions 10-May-19.
Java Basics Exception Handling.
Presentation transcript:

What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic rules of a Java program.

Exception Handling Essentially exception handling is Java’s method of dealing with programming errors. –Java will attempt to handle an error rather than just abort the program. –Exception handling is intended for error processing that is an infrequent activity.

Exception Handling Exception handling is intended to deal with synchronous errors like divide by zero, and array index out of bounds. Exception handling is NOT intended to deal with asynchronous errors such as mouse clicks, network messaging, and keyboard strokes.

Exception Handling Using exception handling is the preferred way to handle error conditions in your program. Exception handling allows you to divide your program into separate sections for the normal case and for the exception case.

Exception Handling Exception handling should be used only in situations where your program is able to recover from the problem and continue running.

Exception Handling Java throws an exception when it cannot complete it’s normal processing. –Array out of bounds, or divide by zero.

Exception Handling Examples –Divide By Zero –Array Out of Bounds

Exception Handling When Java throws an exception, it expects the exception to be caught. –When an exception is caught it means that the error is handled and the program can continue to execute.

Exception Handling If an exception is thrown but your program does not catch the exception, then your program will abort. If an exception is thrown and your program catches the exception, then your program will continue to execute.

Exception Handling There is an Java object class called Exception. –This class or some subclass of Exception are used to catch exceptions in a program.

Types of Exceptions A runtime exception occurs when your program is running. They occur in the Java runtime system. –An example is a divide by zero exception and array index out of bounds. –This type of exception can occur anywhere in your program.

Types of Exceptions A checked exception is verified by the Java compiler to be caught or ignored by any method that the exception can occur in. –A method ignores an exception by throwing the exception in the method declaration. public void someMethod () throws someException {...}

Types of Exceptions If a checked exception is not thrown then the method must catch the exception and deal with it. Checked exceptions are found at compile time and runtime exceptions are found while your program is running.

The Exception Class An exception is an object of the Exception class or one of its subclasses. –The constructor methods for the Exception class are: Exception() and Exception(String s) The second constructor provides an error string as a parameter to the exception object.

The Exception Class The Exception class also contains two methods which are very useful. –The getMessage() method returns the error string stored in the class. –The printStackTrace() method prints the list of methods calls that occurred before the exception.

Throwing an Exception Any Java method can throw any of the pre-defined exception classes. –Defined exception classes include: NoClassDefFound, OutOfMemoryError, StackOverFlowError, ClassNotFoundException, InstantiationException, NullPointerException, NoSuchMethodException, SecurityException, NumberFormatException, EOFException, FileNotFoundException, ArrayIndexOutOfBoundsException, etc.

Throwing an Exception You can also throw an exception that you define. –You throw and catch the exception you define just as you would a predefined exception. –An exception you define must be a subclass of the Exception class.

Throwing an Exception Defining your own exception public class CS2Exception extends Exception { public CS2Exception () { super(“This is the CS2 Exception. You did something wrong!!”); } // end of CS2Exception method } // end of CS2Exception Class

Throwing an Exception To throw an exception you need to use the throw statement in your method. throw new CS2Exception(); –The place where your program encounters this statement is called the throw point

Throwing an Exception If you decide that methodX will throw an exception, then any method that calls methodX must either catch the exception or re-throw it.

Catching Exceptions Every exception thrown by a program must be caught in the program. –The exception must be caught in the method that threw the exception or some other method in the method calling chain. –If an exception is thrown and not caught by the program, then the program will abort.

Try/Catch Blocks A Try/Catch Block is used to catch an exception in your program. –The try section goes around the code that will potentially throw an exception. There is only one try block in each try/catch block combination.

Try/Catch Blocks –The catch section goes around the code that will potentially catch the exception and handle it. There can be multiple catch blocks for each try block. If there are multiple catch blocks, each one catches a different exception. –There may also be a finally block that always executes.

Try/Catch Blocks The basic structure is: try { some executable statements; } catch (SomeException e) { some exception handlers; } finally { some executable statements; }

Example Handling a Divide by Zero exception. Propagate User Exception.

The Exception Hierarchy The Exception class is the highest class in the hierarchy. It is the parent class of all other exceptions. –It is also the generic exception which will catch every possible exception.

The Exception Hierarchy It can become tedious to catch every possible exception in your program, especially if it is a large program. –The most important exceptions should be caught separately and then the less important exceptions can be caught using the Exception class. –Remember to put the individual catch statements first though. Why?

Exception Handling It is possible that a method may handle an exception but also re-throw the exception. –This is usually done if there is further processing to be completed by a higher- level catch block.

A Final Example NegativeParameterException Class ParameterException Class ParameterTooLargeException Class TestExceptions