Exception Handling By: Thomas Fasciano. What is an Exception?  An error condition that occurs during the execution of a Java Program.

Slides:



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

Exception Handling. Introduction Errors can be dealt with at place error occurs –Easy to see if proper error checking implemented –Harder to read application.
Exceptions & exception handling Use sparingly. Things you can do with exceptions: 1. Define a new exception class. 2. Create an exception instance. 3.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
CS102--Object Oriented Programming
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
1 Week 11 l Basic Exception Handling »the mechanics of exceptions l Defining and Using Exceptions »some "simple" cases l Reality Check »guidelines for.
Exception Handling Chapter 8. Outline Basic Exception Handling Defining Exception Classes Using Exception Classes.
Java I/O Java I/O is based on input streams and output streams. All input and output are defined in the Java IO package. 1.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
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 Exceptions. Types of exceptions  Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Objectives Understanding what an exception is Understanding the heirarchy of exception classes Learn the types of exception and how to catch and handle.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
Exceptions and Exception Handling (continued) Carl Alphonce CSE116.
Exception handling Dealing with life’s little surprises.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch Announcements l Project 6 now out. »Milestone due Oct. 24th »Final project.
Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now.
Chapter 15 Strings String::Concat String::CompareTo, Equals, == If( string1 == S”Hello”) String1->Equals(S”Hello”) String1->CompareTo(S”Hello”) CompareTo.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Exception Handling Recitation – 10/(23,24)/2008 CS 180 Department of Computer Science, Purdue University.
CSE 11 February 11, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
Exception Handling (Chapter 8) CS 180 Recitation - February 29, 2008 Department of Computer Science Purdue University.
Introduction to Computer Programming Error Handling.
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 prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
COMP Exception Handling Yi Hong June 10, 2015.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Java I/O Java I/O is based on input streams and output streams. All input and output are defined in the Java IO package. 1.
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.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
EXCEPTIONS There's an exception to every rule.. 2 Introduction: Methods  The signature of a method includes  access control modifier  return type 
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Lecturer: Dr. AJ Bieszczad Chapter 8 COMP 150: Introduction to Object-Oriented Programming 8-1 l Basic Exception Handling »the mechanics of exceptions.
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.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
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.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Chapter 10 – Exception Handling
Introduction to Exceptions in Java
Introduction to Exceptions in Java
Announcements/Reminders
Exception Handling Chapter 9.
Exception Handling Chapter 8 Basic Exception Handling
Exceptions & exception handling
CMSC 202 Exceptions.
Exceptions & exception handling
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
TRY CATCH BLOCK By Kosala Rajapaksha.
Exception Handling Chapter 9 Edited by JJ.
Go to pollev.com/cse143.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Tutorial Exceptions Handling.
Exception Handling Chapter 8 Basic Exception Handling
CMSC 202 Exceptions.
Basic Exception Handling
Presentation transcript:

Exception Handling By: Thomas Fasciano

What is an Exception?  An error condition that occurs during the execution of a Java Program

Common Exceptions in Java  ArithmeticException  NullPointerException  ClassCastException  ArrayIndexOutOfBoundsException  IndexOutOfBoundsException  NoSuchElementException  IllegalStateException

Cont.  The exceptions on the previous slide are all exception most programmers have run into  All are defined Exception classes within the Java language

What Exceptions Do  Point out errors within code  Example: NullPointerException, points out when the object being reference contains a null value when it should not.

Exception Syntax  Starts out with a try block  Inside try block is the code to try  At point error occurs an Exception is thrown through a throw statement  The try block is closed and a catch block immediately follows  The catch block is the code that occurs if the Exception is thrown

Psuedocode try{ code to try decision making statement { throw new Exception()} action taken if Exception not thrown } catch(Exception e) { action taken if Exception is thrown }

Flow of Code try{ code to try decision making statement{ throw new Exception()} action taken if Exception not thrown } catch(Exception e) { action taken if Exception is thrown } If Decision making statement is false Completes code inside Try block and skips over the catch block If Decision making statement is true After Exception is thrown, skips down to the catch block

Example Code try{ Scanner keyboard = new Scanner(System.in); System.out.println("Enter number of glasses"); int glasses = keyboard.nextInt(); if (glasses == 0) throw new Exception("Can not have 0 glasses"); System.out.println("Oz. or milk per glass for 1 gallon of milk: " + (16.0/glasses)); } catch(Exception e) { System.out.println("Exception was thrown"); System.out.println(e.getMessage());} This example assumes you cannot have 0 glasses

Inside try Block  Where the code to “try out” is placed  Where the decision is made if an Exception needs to be thrown or not  If Exception is thrown, any code after the throw statement is not executed  If Exception is not thrown, all code within try block is executed

throw Statement Syntax for a throw statement is as follows throw new Exception(String_Argument ); The String_Arguement can be called later in the catch block Exception can be replaced by a more specific Exception with a predetermined String_Argument and/or specific code

catch Block  This is the code executed if an exception is thrown  Sometimes it will end the program with System.exit(0)  Or it can perform some specific code depending on the program  Can recall the String_Argument placed within the constructor of the Exception  To recall, the method e.getMessage() is used

Making your own Exception Class  First, any exception class must extend the predefined Java class Exception  Only the constructor methods are defined  All other methods are inherited from the class Exception

Example exception Class public class DivideByZeroException extends Exception { public DivideByZeroException() { super("Dividing by zero"); } public DivideByZeroException(String message) { super(message); }} The default message returned with the getMessage() method will be “Dividing by zero” if this is the exception thrown

Example Code with new exception class try{ Scanner keyboard = new Scanner(System.in); System.out.println("Enter number of glasses"); int glasses = keyboard.nextInt(); if (glasses == 0) throw new DivideByZeroException(); System.out.println("Oz. or milk per glass for 1 gallon of milk: " + (16.0/glasses)); } catch(DivideByZeroException e) { System.out.println("Exception was thrown"); System.out.println(e.getMessage());} If the exception is thrown the output will be: Exception was thrown Dividing by zero