Java
Java intro
Java basics
Java variables
Java conditionals
Java loops
Java arrays
Java OOP 1
Java OOP 2
Java interfaces
Java Strings
Java user input
Java exceptions
Java packages
Java rand numbers
Java GUI's
Java GUI layout
Java events
Java applets
Java graphics
Java sounds
Java summary

Programming

Programming intro

Markup

First webpage guide HTML
XHTML

Style & Layout

CSS

Browser scripting

Javascript
VBScript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

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

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:

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.

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments