Exception handling in java

July 12, 2016 by S4 R

Filed under java

Last modified July 12, 2016

Exception handling in java

An exception or exceptional event is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and program/Application terminates abnormally, which is not recommended, therefore these exceptions are to be handled.

Exception handling

Exception handling

An exception can occur for many different reasons, below given are some scenarios where exception occurs.

  • A user has entered invalid data.
  • A file that needs to be opened cannot be found.
  • A network connection has been lost in the middle of communications or the JVM has run out of memory.

Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner.

There are three types of  Exception handling

  • Checked exceptions: A checked exception is an exception that occurs at the compile time, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation, the Programmer should take care of  these exceptions.

For example,  FileReader class in your program to read data from a file, if the file specified in its constructor doesn’t exist, then an FileNotFoundException occurs, and compiler prompts the programmer to handle the exception.

import java.io.File;

import java.io.FileReader;

 

public class FilenotFound_Demo {

 

public static void main(String args[]){

File file=new File(“E://file.txt”);

FileReader fr = new FileReader(file);

}}

The above program  will get exceptions as shown below.

C:\>javac FilenotFound_Demo.java

FilenotFound_Demo.java:8: error: unreported exception File Not Found Exception; must be caught or declared to be thrown

File Reader fr = new File Reader(file);

^1 error

Note: Since the methods read() and close() of File Reader class throws IO Exception, observe that compiler notifies to handle IO Exception, along with File Not Found Exception.

  • Unchecked exceptions: An Unchecked exception is an exception that occurs at the time of execution, these are also called as Runtime Exceptions, these include programming bugs, such as logic errors or improper use of an API. runtime exceptions are ignored at the time of compilation.

For example,  declared an array of size 5 in  program, and trying to call the 6th element of the array then an ArrayIndexOutOfBound Exceptionexception occurs.

public class Unchecked_Demo {

 

public static void main(String args[]){

int num[]={1,2,3,4};

System.out.println(num[5]);

}}

compile and execute the above program  will get exception as shown below.

Exception in thread”main” java.lang.ArrayIndexOutOfBoundsException: 5

at Exceptions.Unchecked_Demo.main(Unchecked_Demo.java:8)

  • Errors: These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation.

Exception Hierarchy:

All exception classes are subtypes of the java.lang.Exception class. The exception class is a subclass of the Throwable class. Other than the exception class there is another subclass called Error which is derived from the Throwable class.

Errors are abnormal conditions that happen in case of severe failures, these are not handled by the java programs. Errors are generated to indicate errors generated by the runtime environment. Example : JVM is out of Memory. Normally programs cannot recover from errors.

The Exception class has two main subclasses: IOException class and RuntimeException Class.

Leave a Comment