17 Exception handling

Download Report

Transcript 17 Exception handling

17 Exception handling
CE00858-1:
Fundamental Programming Techniques
April 16
17 Exception handling
1
Objectives
In this session, we will:
•
•
•
•
April 16
introduce exceptions
consider when exceptions occur
write code to handle exceptions
compare checked and unchecked exceptions
17
FPT
Exception
Week 5 Session
handling
1
2
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
April 16
17 Exception handling
3
Exceptions at run-time
• when an exception occurs, the run-time system
displays details:
• name of exception:
• InputMismatchException
• location where exception occurred:
• stack trace
April 16
17 Exception handling
4
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
April 16
17 Exception handling
5
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
April 16
17 Exception handling
6
//program to divide 2 integers
import java.util.*;
public class Division
Division.java
{
public static void main (String [] args)
{
Scanner kybd = new Scanner(System.in);
System.out.println("Enter 2 integers: ");
try
{
block containing potential
problem
int num1 = kybd.nextInt();
int num2 = kybd.nextInt();
int ans = num1 / num2;
System.out.println("Answer is: " + ans);
}
catch (InputMismatchException e)
{
block executed if problem
with input
System.out.println(e.toString());
}
catch (ArithmeticException e)
{
}
}
April 16
}
System.out.println(e.toString());
17 Exception handling
block executed if problem
with division
7
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
April 16
17 Exception handling
8
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
April 16
17 Exception handling
9
GetNum.java
import java.util.*;
public class GetNum
{
public static void main (String [] args)
{
}
int validNum = validateNum();
System.out.println(validNum);
//method to input valid data
public static int validateNum()
{
Scanner kybd = new Scanner(System.in);
System.out.print("Please enter an integer: ");
while (true)
{
try
{
}
loop forever
int num = kybd.nextInt();
return num;
catch (InputMismatchException e)
{
}
}
April 16
}
}
return num if valid
and finish loop
System.out.print("Invalid input, try again: ");
String invalidInput = kybd.next();
clear input buffer
of invalid data
17 Exception handling
10
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
April 16
17 Exception handling
11
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
April 16
17 Exception handling
12