1 / 12

17 Exception handling

17 Exception handling. CE00858-1: Fundamental Programming Techniques. September 14. 1. Objectives. In this session, we will: introduce exceptions consider when exceptions occur write code to handle exceptions compare checked and unchecked exceptions. September 14. FPT Week 5 Session 1.

darryl
Télécharger la présentation

17 Exception handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 17 Exception handling CE00858-1: Fundamental Programming Techniques September 14 17 Exception handling 1

  2. Objectives In this session, we will: • introduce exceptions • consider when exceptions occur • write code to handle exceptions • compare checked and unchecked exceptions September 14 17 Exception handling FPT Week 5 Session 1 2

  3. Exceptions • exceptions are generated when something goes wrong • dividing a number by zero • ArithmeticException • accessing an array element that doesn't exist • ArrayIndexOutOfBoundsException • reading data of the wrong type with a Scanner object • InputMismatchException • opening a data file that doesn’t exist • FileNotFoundException • reading past the end of a file • EOFException 17 Exception handling

  4. Exceptions at run-time • when an exception occurs, the run-time system displays details: • name of exception: • InputMismatchException • location where exception occurred: • stack trace 17 Exception handling

  5. Handling exceptions • can avoid program crash by handling the exception • code that could cause an error is put in a try block • code to handle the exception is put in a catch block • catch block is executed immediately an exception occurs • multiple catch blocks can deal with different exceptions that may occur • optional finally block is executed whether or not exception occurs • the catch block is passed details of the exception that occurred • the toString() method can be used to display information about the exception 17 Exception handling

  6. Exception example – division • program required to: • prompt the user for 2 integers • output the results of dividing the first by the second • analysis: • what data is used? • num1: integer input by user • num2: integer input by user • what operations are performed? • input num1 and num2 • output num1 / num2 • what exceptions might occur? • user may input data of incorrect type • second number may be 0 17 Exception handling

  7. Division.java //program to divide 2 integers import java.util.*; public class Division { public static void main (String [] args) { Scanner kybd = new Scanner(System.in); System.out.println("Enter 2 integers: "); try { int num1 = kybd.nextInt(); int num2 = kybd.nextInt(); int ans = num1 / num2; System.out.println("Answer is: " + ans); } catch (InputMismatchException e) { System.out.println(e.toString()); } catch (ArithmeticException e) { System.out.println(e.toString()); } } } block containing potential problem block executed if problem with input block executed if problem with division 17 Exception handling

  8. Continuing processing example - getNum • sometimes may wish to continue processing after an exception • program to: • prompt user for integer • output error if wrong type • continue until valid data input 17 Exception handling

  9. getNum analysis • what data is used? • num: integer input by user • what operations are performed? • iteration needed to read data until valid input • how many times is loop executed? • loop executed until valid data input • what operations are repeated inside loop? • read data • check for exception • what exceptions might occur? • user may input data of incorrect type 17 Exception handling

  10. GetNum.java import java.util.*; public class GetNum { public static void main (String [] args) { intvalidNum = validateNum(); System.out.println(validNum); } //method to input valid data public static intvalidateNum() { Scanner kybd = new Scanner(System.in); System.out.print("Please enter an integer: "); while (true) { try { int num = kybd.nextInt(); return num; } catch (InputMismatchException e) { System.out.print("Invalid input, try again: "); String invalidInput = kybd.next(); } } } } loop forever return num if valid and finish loop clear input buffer of invalid data 17 Exception handling

  11. Unchecked and checked exceptions • unchecked exceptions: • can be avoided within code • testing bounds before accessing array • preventing division by 0 • checking data type when reading using Scanner • try...catch block not required • checked exceptions: • compiler error if not dealt with in try...catch block • opening non-existent file • reading past end of file 17 Exception handling

  12. Summary In this session we have: • used exceptions and seen when they occur • written code to handle exceptions • looked at the differences between checked and unchecked exceptions In the next session we will: • consider file handling September 14 17 Exception handling 12

More Related