Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Yoshi
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.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
Chapter 12 By Tony Gaddis Modified by Elizabeth Adams Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
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.
Exception Handling Topics We will discuss the following main topics: – Handling Exceptions – Throwing Exceptions – More about Input/Output Streams.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
E XCEPTION H ANDLING Chapter 11 C S 442: A DVANCED J AVA P ROGRAMMING.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
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.
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.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
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.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
Object Oriented Programming
CIS 270—Application Development II Chapter 13—Exception Handling.
Chapter 12: Exception Handling
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
Slides Credit Umair Javed LUMS Web Application Development.
Tues. Nov. 25.  An exception is an unexpected error or condition, such as ◦ You issue a command to read a file from a disk, but the file does not exist.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects CS 146 Class Notes Fall 10.
Exception. Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Unit 4 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Exceptions, handling exceptions & message boxes Year 11 Information Technology.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
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.
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.
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.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Lecture 18B Exception Handling and Richard Gesick.
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.
Exceptions in the Java programming language J. W. Rider.
Java Programming Fifth Edition
Exception Handling Chapter 9.
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
Exception Handling and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
CSE 143 Java Exceptions 1/18/2019.
Java Exceptions Dan Fleck CS211.
Presentation transcript:

Java Programming Exceptions Handling

Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch multiple Exceptions Use the finally block Understand the limitations of traditional error handling

Specify the Exceptions a method can throw Handle Exceptions uniquely with each catch Trace Exceptions through the call stack Create your own Exceptions

Learning about Exceptions Exception- An unexpected or error condition  For example: You issue a command to read a file from a disk, but the file does not exist You write data to a disk but the disk is full or unformatted The program attempts to divide by zero

Learning about Exceptions Exception Handling- The object-oriented techniques to manage such errors comprise the group of methods know as exception handling  There are two basic classes of errors Error and Exception  Both classes descend from the Throwable class  You must expect the unexpected

Throwable Exceptions (Checked/unchecked) AWTErrorThreadDeath Error (Unchecked) RuntimeException (Unchecked) IOException (Checked) Exception Class Hierarchy OutOfMemory Checked Exception: “must catch or declare”

Learning about Exceptions Error class- Represents more serious errors from which your program usually cannot recover  For example, spelling a class name incorrectly or storing a required class in the wrong folder

Learning about Exceptions Exception class- Comprise less serious errors that represent unusual conditions that arise while a program is running, and from which the program can recover  Examples of Exception class errors include using an invalid array subscript or performing certain illegal arithmetic operations

Trying Code and Catching Exceptions try block- A block of code you attempt to execute, while acknowledging that an Exception might occur Consists of the following elements:  The keyword try  An opening curly brace  Statements that might cause exceptions  A closing curly brace

Trying Code and Catching Exceptions catch block- Must code at least one catch block immediately following a try block  A segment of code that can handle an Exception that might be thrown by the try block that precedes it

Creating a catch block Create a catch block by typing the following elements:  The keyword catch  An opening parenthesis  An Exception type  A name for an instance of the Exception type  A closing parenthesis  An opening curly brace  Statements that take the action you want to use to handle the error condition  A closing curly brace

General format of a try…catch pair

Using the Exception GetMessage() Method getMessage() method- To retrieve Java's message about any Throwable Exception named someException  Code someException.getMessage()

Throwing and Catching Multiple Exceptions  Can place as many statements as you need within a try block  Can catch as many Exceptions as you want  If you try more than one statement, only the first error-generating statement throws an Exception As soon as the Exception occurs, the logic transfers to the catch block

Throwing and Catching Multiple Exceptions  Multiple catch blocks are examined in sequence until a match is found for the type of Exception that occurred The match catch block executes and each remaining catch block is bypassed

Using the finally Block The finally block contains actions you must perform at the end of a try…catch sequence Code within a finally block executes whether or not the preceding try block identifies exceptions The finally block performs clean-up tasks that must happen

Understanding the Limitations of Traditional Error Handling Before object-oriented programming, potential errors were handled using error- prone methods Object-oriented exception handling allows you to isolate error-handling code

Specifying the Exceptions a Method can Throw When you write a method that might throw an Exception, you can type the clause throws Exception after the method header to indicate the type of exception that might be thrown When you use any method you must know  The method’s name  The method’s return type  The type and number of arguments it requires  The type and number of Exceptions the method throws

Specifying the Exceptions a Method can Throw Method's signature- A method's header, including its name, any arguments, and any throws clause

Handling Exceptions Uniquely with Each catch When you use a class and one of its methods throws an Exception, the class that throws the Exception does not have to catch it Your calling program can catch the Exception, and then you can decide what to do with it

Tracing Exceptions Through the Call Stack Call stack- Where the computer stores the list of locations to which the system must return When a method throws an Exception, and if the method does not catch the Exception, the Exception is thrown to the next method up the call stack You can display this list using the printStackTrace() method

Creating Your Own Exceptions Java provides over 40 categories of Exceptions that you can throw in your programs Java also allows you to create your own Exceptions You should extend your Exceptions from the Exception class When you create an Exception, it's conventional to end its name with Exception