Tutorial search
Cool Stuff
How would you like to MASTER graphic design by next week?
Click here to find out how
Photoshop Templates
Java Tutorials
Tutorials
Stuff
Affiliates
Java exceptions - Java tutorial


Introduction
An Exception, It can be defined as an abnormal event that occurs during program execution and disrupts the normal flow of instructions. The abnormal event can be an error in the program.
Errors in a java program are categorized into two groups:
- Compile-time errors occur when you do not follow the syntax of a programming language.
- Run-time errors occur during the execution of a program.
Concepts of Exceptions
An exception is a run-time error that occurs during the exception of a java program.
For e.g.
If you divide a number by zero or open a file that does not exist, an exception is raised.
In java, exceptions can be handled either by the java run-time system or by a user-defined code. When a run-time error occurs, an exception is thrown.
The unexpected situations that may occur during program execution are:
- Running out of memory
- Resource allocation errors
- Inability to find files
- Problems in network connectivity
Exception Classes
To handle exceptions, the java run-time system searches for an exception-handler. In java, a catch statement is an exception-handler that is used to handle an exception. The search for an exception-handler begins with the method in which the exception is raised.
If no appropriate exception-handler is found, the java run-time system searches the exception-handler in the next higher method hierarchy. The type of exception handled by the exception-handler should match the type of exception thrown.
The java run-time system proceeds with the normal execution of the program after an exception gets handled. If no appropriate exception-handler is found by the java run-time system, the program is terminated.

Exception Hierarchy
Throwable class
The Throwable class is a subclass of the Object class. The throwable class is the super class of all the exception objects that are thrown in java.
Syntax to declare a constructor for the Throwable class:
You can also declare a constructor with user-defined message.
You can throw only those exception objects that are derived from the Throwable class.
Exception class
The Exception class has various subclasses, such as
- ClassNotFoundException – exception is thrown when a class is being referred, but no definition for the class with the specified name is found.
- IllegalAccessException – exception is thrown when a particular method is not found
- RuntimeException – exception handles the exceptions that are raised in the programs during run time.
Error class
The Error class defines exceptions related to the java run-time environment.
For e.g.
OutOfMemoryError – is an error that occurs when there is insufficient system memory to execute a program.
The Error class consists of two types of constructors,
- One without any error message.
- Another with an error message.
Syntax to declare the constructor of the Error class with no message:
Syntax to declare the constructor of the Error Class with message:
Built-in Exceptions
The built-in exceptions in java are categorized on the basis of whether the exception is handled by the java Compiler or not. Java consists of the following categories of built-in exceptions:
- Checked Exceptions
- Unchecked Exceptions
Checked Exceptions
Checked exceptions are the objects of the Exception class or any of its subclasses excluding the Runtime Exception class. Checked Exceptions are the invalid conditions that occur in a java program due to invalid user input, network connectivity problem or database problems.
Java uses the try-catch block to handle the checked exceptions. The statements within a program that throw an exception are placed in the try block. You associate an exception-handler with the try block by providing one or more catch handlers immediately after the try block.
Various checked exceptions defined in the java.lang.package are,
- ClassNotFoundException
- IllegalAccessException
- InstantiationException
- NoSuchMethodException
Unchecked Exceptions
Unchecked exceptions are the run-time errors that occur because of programming errors, such as invalid arguments passed to a public method. The java complier does not check the unchecked exceptions during program compilation.
Various Unchecked Exceptions are,
- ArithmeticException
- ArrayIndexOutOfBoundsException
- ArrayStoreException
- ClassCastException
- IllegalArgumentException
- NegativeArraySizeException
- NullPointerException
- NumberFormatException
Implementing Exception-Handling
When an unexpected error occurs in a method,
- Java creates an object of the type Exception
- Java sends it to the program by throwing the exception
- The Exception object contains information about the type of error and the state of the program when the exception occurred.
- You need to handle the exception using an exception-handler and process the exception.
