Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Exception Handling By: Thomas Fasciano

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

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

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

5 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.

6 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

7 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 }

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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


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

Similar presentations


Ads by Google