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.

Slides:



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

Topics Introduction Types of Errors Exceptions Exception Handling
Yoshi
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
CS102--Object Oriented Programming
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
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.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
Index Exception handling Exception In Java Exception Types
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:
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions Chapter 18 Programs: DriverException2 DriverException TestAgeInputException.
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. Introduction An exception is an abnormal condition that arises in a code sequence at run time. In computer languages that do not support.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exceptions Handling.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
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.
06 - Exceptions. 2 ©S. Uchitel, 2004 A familiar sight? Bluescreen.scr.
©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.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
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.
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.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
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
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
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
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.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
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.
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.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
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:
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.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Java Programming Fifth Edition
Chapter 10 – Exception Handling
Introduction to Exceptions in Java
COMPSCI 230 S Programming Techniques
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
Exception Handling and Reading / Writing Files
Managing Errors and Exceptions
Lecture 11 Objectives Learn what an exception is.
Exceptions 25-Apr-19.
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.
Exceptions 22-Apr-19.
Exceptions 10-May-19.
Java Basics Exception Handling.
Exceptions 5-Jul-19.
Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

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 there ◦ You attempt to write data to a disk, but the disk is full or unformatted ◦ Your program asks for user input, but the user enters invalid data ◦ The program attempts to divide a value by 0

 These are the object-oriented methods to manage such errors  Java has two basic classes of errors: Error and Exception  Both of these classes descend from the Throwable class, as shown next

 Object ◦ Throwable  Exception  IOException  Runtime Exception  ArithmeticException  IndexOutOfBoundsException  ArrayIndexOutOfBoundsException  Others  Error  VirtualMachineError  OutofMemoryError  InternalError  Others…

 These are serious errors from which your program cannot recover  Such as, a misspelled class name or a class stored in the wrong folder  If the program cannot find the class, an Error condition occurs  If the program runs out of memory, an Error condition occurs

 These are less serious errors that represent unusual conditions that arise while the program is running and from which the program can recover  Examples include using an invalid array subscript or performing certain illegal arithmetic operations  Your program can determine the type of error by examining the code of the error returned by Java

C:\Java\Java xyz.java error: cannot read xyz.java 1 error C:\Java\>_  In this case there is no program code you could write that could fix or prevent the error message

 When the program generates an exception message, it produces a different type of message  An exception message is one that could have been prevented by writing specific methods in the program  Consider the following program…

public class MathMistake { public static void main(String[] args) { int num = 13, denom = 0, result; result = num / denom; }

C:\Java\java MathMistake Exception in thread “main” java.lang.ArithmeticException: / by zero at MathMistake.main C:\Java>_  The Exception is a java.lang.ArithmeticException which is one of many subclasses of Exception  You are told the type of error as well as the line number of the error

 You can simply let the program terminate  The termination is abrupt and unforgiving  This might annoy the user  Or the termination could be disastrous such as in an air traffic control situation

 Consider the following arithmetic (division by 0) error and its various traditional ways of handling the error

public class MathMistake { public static void main(String[] args) { int num = 13, denom = 0, result; if(denom == 0) System.exit(1); result = num / denom; }

 When you use System.exit(), the current application ends and control returns to the operating system  The convention is to return a value of 1 if an error is causing the termination; otherwise, a value of 0 is returnedunder normal termination  Using System.exit() circumvents displaying the error message shown above because the program ends before the the division by zero occurs

 When you write code to detect exceptions, you ‘try’ a procedure that might cause an error  A method that detects an exception ‘ throws an exception,’ and the block of code that processes the error ‘catches the exception.’  Programs that can handle exceptions are said to be more fault tolerant and robust ◦ Fault-tolerant apps are designed so that they continue to operate, possibly at a reduced level

 When you create a segment of code in which something might go wrong, you place the code in a try block, which is a block of code you attempt to execute while acknowledging that an exception might occur

 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  The statements that take the action you want  A closing curly brace

returnType methodName(optional arguments) { optional statements prior to code that is tried try { //statements that might generate an exception } catch(Exception someException) { // actions to take if exception occurs } // optional statements that occur after try }

 In the slide above, someException represents an object of the Exception class or any of its subclasses  If an exception occurs during the execution of the try block, the statements in the catch block execute  If no exception occurs within the try block, the catch block does not execute  Either way the statements following the catch block execute normally

public class MathMistakeCaught { public static void main(String[] args) { int num = 13, denom = 0, result; try { result = num / denom; } catch(ArithmeticException mistake) { System.out.println("Attempt to divide by zero!"); }

 C:\Java>java MathMistakeCaught  Attempt to divide by zero!  C:\Java>

 If you want to send error messages to a different location from ‘normal’ you can use System.err instead of System.out

 Lots of different types of errors could have occurred within the try block  You can have the system print the type of error with the following code

public class MathMistakeCaught2 { public static void main(String[] args) { int num = 13, denom = 0, result; try { result = num / denom; } catch(ArithmeticException mistake) { System.out.println(mistake.getMessage()); }

 Uses the getMessage() method to generate the message that ‘comes with’ the caught ArithmeticException argument to the catch block

C:\Java>java MathMistakeCaught2 / by zero C:\Java>_

try { result = num / denom; } catch(ArithmeticException mistake) { result = num/1; System.out.println(mistake.getMessage()); }

 Holds a valid value—either the code in the try block worked, or the code in the catch block assigned a valid value to result if the try block did not work

 The catch blocks are written so that they look like methods

 You can place as many statements as you need within a try block and you can catch as man Exceptions as you want  However, the first such Exception will cause the logic to leave the try block and to enter the catch block  Thus, the first error-generating statement throws an Exception  When a program contains multiple catch blocks, they are examined in sequence until a match is found for the type of Exception that occurred

 Then, the matching catch block executes and each remaining catch block is bypassed  For example, consider the code below

public class TwoMistakes { public static void main(String[] args) { int num[] = {4, 0, 0}; try { num[2] = num[0] / num[1]; num[2] = num[3] / num[0]; } catch(ArithmeticException e) { System.out.println("Arithmetic error"); } catch(IndexOutOfBoundsException e) { System.out.println("Out of bounds error"); } System.out.println("End of program"); }

 C:\Java\java TwoMistakes  Arithmetic error  End of program  C:\Java>_  Note that the second statement in the try block is never attempted and the second catch block is skipped

public class TwoMistakes2 { public static void main(String[] args) { int num[] = {4, 0, 0}; try { num[2] = num[0] / 10; num[2] = num[3] / num[0]; } catch(ArithmeticException e) { System.out.println("Arithmetic error"); } catch(IndexOutOfBoundsException e) { System.out.println("Out of bounds error"); } System.out.println("End of program"); }

 C:\Java\java TwoMistakes2  Out of bounds error  End of program  C:\Java>_  Again, the try block is abandoned, the first catch block is examined and found unsuitable because it does not catch an IndexOutOfBoundsException  The program proceeds to the second catch block

 The following program uses only one catch block to catch both ArithmeticExceptions and IndexOutOfBounds exceptions  The above exceptions are both subclasses of Exception  Therefore, the catch blocks shown above can be replaced with a single generic catch block, as shown below

public class TwoMistakes3 { public static void main(String[] args) { int num[] = {4, 0, 0}; try { num[2] = num[0] / num[1]; num[2] = num[3] / num[0]; } catch( Exception e) { System.out.println(e.getMessage()); }

 Will catch all exceptions  When either an arithmetic exception or array index exception occurs, the thrown exception is ‘promoted’ to an Exception error in the catch block  Because an Exception is Throwable, you can use the Throwable class getMessage() method

 If you place a generic catch block first and then specific catch blocks afterwards, the specific catch blocks will possess code that is unreachable because the generic catch block will catch all exceptions before they can be tested against specific catch blocks  Unreachable code statements are program statements that can never execute under any circumstances.  Try blocks should not throw more than four exception types

 finally blocks follow catch blocks and are used to cleanup after try and catch blocks regardless of whether an exception occurred or not

Try { // statements of try } catch(Exception e) { // actions taken if exception was thrown } finally { // actions taken whether catch block executed or not }

 There is a System.exit(); statement in either the try block or the catch block  An unplanned Exception occurs  When an unplanned Exception occurs, the application is stopped and control is returned to the operating system where the Exception is handled—the program is abandoned

try { // Open the file // Read the file // Place the file data in an array // Calculate an average from the data // Display the average } catch(IOException e) { // Issue an error message // System exit } finally { // if file is open, close it }

 The advantage of exception handling is that it enables you to create a fix-up for the exception and to permit the program to continue  It also simplifies exception handling overall

call methodA() If methodA() worked { call methodB() if methodB() worked { call methodC() if methodC() worked Everything is OK so print final result else Set errorCode to ‘C’ } else Set errorCodee to ‘B” } else Set errorCode to ‘A’ }

try { call methodA() and maybe throw an exception call methodB() and maybe throw an exception call methodC() and maybe throw an exception } Catch(methodA()’s error) { set errorCode to ‘A’ } Catch(methodB()’s error) { set errorCode to ‘B’ } Catch(methodC()’s error) { set errorCode to ‘C’ }

 Logic is much simplier  Reusability of the method is much better  If a method throws an exception that will not be caught inside the method, you must use the keyword throws followed by an Exception type in the method header  Consider the following code…

Public class PriceList { private static final double[] price = {15.99,27.88,34.56,45.89}; public static void displayPrice(int item) throws IndexOutOfBoundsException { System.out.println(“The price is” + price[item]; }

public class PriceListApplication1 { public static void main(String[] args) { int item = 4; try { PriceList.displayPrice(item); } catch(IndexOutOfBoundsException e) { System.out.println("Price is $0"); }

import javax.swing.*; public class PriceListApplication2 { public static void main(String[] args) { int item = 4; try { PriceList.displayPrice(item); } catch(IndexOutOfBoundsException e) { while(item 3) { String answer = JOptionPane.showInputDialog(null, "Please reenter a value 0, 1, 2 or 3"); item = Integer.parseInt(answer); } PriceList.displayPrice(item); } System.exit(0); }

 Java’s exceptions come in two types: ◦ Checked exceptions—the type programmers should be detecting and recovering from ◦ Unchecked exceptions—don’t mess with these  Programmers usually cannot anticipate unchecked exceptions, which are ◦ Hardware failures ◦ Logic errors ◦ [programs cannot fix these]  Programs can only fix checked exceptions while it is executing

 All exceptions that you specifically throw are checked exceptions  These descend from the Exception class  These are not runtime exceptions

 Catch it in the method  Declare the exception in your method header’s throws clause  If you use the latter, then any programmer who uses your method must do one of the following: ◦ Catch and handle the exception ◦ Declare the exception in the throws clause of their method

 The method’s return type  The type and number of arguments the method requires  The type and number of Exceptions the method throws  Why don’t you have to know what other methods the method extends from and what other methods the method implements?

 When an exception occurs, Java will trace back through the call stack looking for a catch in each of the methods  If none is found, control returns to the operating system  Consider the following program…

import javax.swing.*; public class DemoStackTrace { public static void main(String[] args) { methodA(); // line 6 } public static void methodA() { System.out.println("In methodA()"); methodB(); // line 11 } public static void methodB() { System.out.println("In methodB()"); methodC(); // line 16 } public static void methodC() { System.out.println("In methodC()"); JFrame frame = null; frame.setVisible(true); // line 22 }

C:\Java>java DemoStackTrace In methodA() In methodB() In methodC() Exception in thread “main” java.lang.nullPointerException at DemoStrackTrace.methodC(DemoStackTrace.java:22) at DemoStrackTrace.methodB(DemoStackTrace.java:16) at DemoStrackTrace.methodA(DemoStackTrace.java:11) at DemoStrackTrace.main(DemoStackTrace.java:6)  C:\Java>

import javax.swing.*; public class DemoStackTrace2 { public static void main(String[] args) { methodA(); // line 6 } public static void methodA() { System.out.println("In methodA()"); try { methodB(); // line 13 } catch(RuntimeException e) { System.out.println("In methodA() - The stack trace:"); e.printStackTrace(); } System.out.println("Method ends normally."); System.out.println("Application could continue here."); } public static void methodB() { System.out.println("In methodB()"); methodC(); // line 26 } public static void methodC() { System.out.println("In methodC()"); JFrame frame = null; frame.setVisible(true); // line 32 }

C:\Java>java DemoStackTrace In methodA() In methodB() In methodC() In methodA() – The stack trace: java.lang.nullPointerException at DemoStrackTrace.methodC(DemoStackTrace.java:32) at DemoStrackTrace.methodB(DemoStackTrace.java:26) at DemoStrackTrace.methodA(DemoStackTrace.java:13) at DemoStrackTrace.main(DemoStackTrace.java:6) Method ends normally. Application could continue here.  C:\Java>

 Java provides over 40 categories of Exceptions that you can use in your programs  But Java cannot anticipate all of the possible exceptions you might want to catch ◦ Ex. A negative bank balance ◦ Ex. Someone tries to access your  To create your own throwable Exception, you must extend a subclass of Throwable.  The class Throwable has two subclasses— Exception and Error

 Exception() – constructs a new exception with null as its detail message  Exception(String message) – constructs a new exception with the specified detail message  Exception(String message, Throwable cause) – constructs a new exception with the specified detail message and cause  Exception(Throwable cause) – constructs a new exception with the specified cause

 public class HighBalanceException extends Exception  {  public HighBalanceException()  {  super("Customer balance is high");  }

 public class CustomerAccount  {  private int acctNum;  private double balance;  public static double HIGH_CREDIT_LIMIT = ;  public CustomerAccount(int num, double bal) throws HighBalanceException  {  acctNum = num;  balance = bal;  if(balance > HIGH_CREDIT_LIMIT)  throw(new HighBalanceException());  }

 import javax.swing.*;  public class UseCustomerAccount  {  public static void main(String[] args)  {  int num;  double balance;  String input;  input = JOptionPane.showInputDialog(null, "Enter account number");  num = Integer.parseInt(input);  input = JOptionPane.showInputDialog(null, "Enter balance due");  balance = Double.parseDouble(input);  try  {  CustomerAccount ca = new CustomerAccount(num, balance);  JOptionPane.showMessageDialog(null, "Customer #" +  num + " has a balance of $" + balance);  }  catch( HighBalanceException hbe)  {  JOptionPane.showMessageDialog(null, "Customer #" +  num + " has a balance higher than the credit limit");  }  System.exit(0);  }

 Assertions are Java language features that help you detect logic errors that do not create runtime errors, but do produce results that are wrong  You use assert statement to create an assertion  The syntax is…  Assert booleanExpression : optionalErrorMessage

import javax.swing.*; class PayRaise { public static void main(String[] args) { double oldPay = 0; double newPay = 0; final double INCREASE = 0.04; final double TAXRATE = 0.28; final double HRSPERWEEK = 40; final double INSURANCE = ; String entry; entry = JOptionPane.showInputDialog(null, "Enter old pay per hour"); oldPay = Double.parseDouble(entry); newPay = oldPay + oldPay * INCREASE; assert oldPay < newPay: "\nOld Pay is " + oldPay + "\nNew pay is $" + newPay + "\nNew pay should be more than old pay"; newPay = (newPay * HRSPERWEEK) - (newPay * TAXRATE) - INSURANCE; assert oldPay < newPay: "Old Pay is " + oldPay + "\nNew pay is $" + newPay + "\nNew pay should be more than old pay"; JOptionPane.showMessageDialog(null, "New net pay is $" + newPay + " per week"); System.exit(0); }

C:\Java>javac –source 1.6 PayRaise.java C:\Java>java –ea Payraise Exception in thread “main” java.lang.AssertioniError: Old Pay is $12.48 New pay is $0.0 New pay should be more than old pay At PayRaise.main C:\Java>

public class CustomerAccount { private int acctNum; private double balance; public static double HIGH_CREDIT_LIMIT = ; public CustomerAccount(int num, double bal) throws HighBalanceException { acctNum = num; balance = bal; if(balance > HIGH_CREDIT_LIMIT) throw(new HighBalanceException()); }

import javax.swing.*; public class DemoStackTrace { public static void main(String[] args) { methodA(); // line 6 } public static void methodA() { System.out.println("In methodA()"); methodB(); // line 11 } public static void methodB() { System.out.println("In methodB()"); methodC(); // line 16 } public static void methodC() { System.out.println("In methodC()"); JFrame frame = null; frame.setVisible(true); // line 22 }