Java exceptions
Exceptions are used to handle situations where a program might crash or something unexpected might occur, such as the user not entering any data when it is required.
This lesson focuses on:
- The various Java exceptions
- The try-catch block
- The try-catch-finally block
The various Java exceptions
Java has several classes that can be used to handle exceptions in various situations.
NOTE: The base class of all the exception classes is the class Exception and it is located in the java.lang package.
Java exceptions:
-
ArithmeticException
The ArithmeticException class is located in the java.lang package. It is used to handle arithmetic exceptions such as when a number is attempted to be divided by zero.
-
NoSuchMethodException
The NoSuchMethodException class is located in the java.lang package. It is used to handle a situation where a method is not found.
-
IOException
The IOException class is located in the java.io package. It is used to handle a situation where an input/output error has occured.
-
FileNotFoundException
The FileNotFoundException class is located in the java.io package. It is used to handle a situation where a file cannot be found.
The try-catch block
The try-catch block is a block which contains one set of code that will execute if there is no error in it and another set of code that will execute if there is an error in the code in the try block.
Syntax:
try{
code to execute;
}
catch(anExceptionThatMayOccur){
code to execute if there is an error in the try block;
}
Example:
import java.io.*;
class GetUserInput{
public static void main(String[] args){
//declare a variable to store user data
String numberEntered = "";
//declare a variable to get user data
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader
(System.in));
//ask the user to enter a number
System.out.println("Enter a number");
try{
//get the data
numberEntered = reader.readLine();
//print the number entered by the user
System.out.println("The number you entered is "
+ Integer.parseInt
(numberEntered));
}
catch(IOException ioe){
//print a message if an
//input/output error occurs
System.out.println("An unexpected
error has occured");
}
catch(NumberFormatException nfe){
//print a message if non-numeric data is entered
System.out.println("You did not
enter numeric data.");
}
}
}
In the above example, the user is asked to enter a number. The try block attempts to print the number entered by the user and will do so if it is indeed numeric data. The first catch block is set to catch an IOException (an exception that occurs if an input/output error occurs). If this exception occurs, this catch block will print a message informing the user of this. The second catch block is set to catch a NumberFormatException (an exception that occurs if data entered by the user was expected to be numeric data and it is not). If this exception occurs, this catch block will print a message informing the user of this.
NOTE: The Integer.parseInt() function is used to convert string data into numeric data. This function is used in the above example because data entered by the user is originally string data. The Integer.parseInt() function is used to convert it into numeric data (if the string is a number or a set of numbers).
The try-catch-finally block
The try-catch-finally block is a block which contains one set of code specified in a try block that will execute if there is no error in it and another set of code that will execute if there is an error in the code in the try block, and a third set of code in a finally block that will execute whether there was an error or not.
Syntax:
try{
code to execute;
}
catch(anExceptionThatMayOccur){
code to execute if there is an error in the try block;
}
finally{
code to execute whether there is an error or not;
}
Example:
import java.io.*;
class GetUserInput{
public static void main(String[] args){
//declare a variable to store user data
String numberEntered = "";
//declare a variable to get user data
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader
(System.in));
//ask the user to enter a number
System.out.println("Enter a number");
try{
//get the data
numberEntered = reader.readLine();
//print the number entered by the user
System.out.println("The number you entered is "
+ Integer.parseInt
(numberEntered));
}
catch(IOException ioe){
//print a message if an
//input/output error occurs
System.out.println("An unexpected
error has occured");
}
catch(NumberFormatException nfe){
//print a message if non-numeric data is entered
System.out.println("You did not
enter numeric data.");
}
finally{
//print a message whether
//there is an error or not
System.out.println("This text will be printed
whether there is an error
or not.");
}
}
}
In the above example, the user is asked to enter a number. The try block attempts to print the number entered by the user and will do so if it is indeed numeric data. The first catch block is set to catch an IOException (an exception that occurs if an input/output error occurs). If this exception occurs, this catch block will print a message informing the user of this. The second catch block is set to catch a NumberFormatException (an exception that occurs if data entered by the user was expected to be numeric data and it is not). If this exception occurs, this catch block will print a message informing the user of this. The finally block contains a message to print whether an error occurs or not.




