Exception handling in Java with example

Exception handling is most important feature in java.In this guide will learn about what is exception, types of exception with example.The exception handling in java is used to maintain flow of application.

Exception:-

An exception is an event that occurs during exception of program that interrupts the normal flow of program.

The parent class of all exception is java.lang.Exception class.When error occur within method ,method create exception object  that contain information about error,type and state and provides to runtime system. Exception throwing means creating an exception object and handles it at runtime.

When exception occurs program gets terminated.Stacks keeps state of all active methods.The call stack is ordered list of methods that had been called to get specific method.

Error:-

Error mainly caused by the environment in which application is running.You will not be able to handle with try catch block.

e.g. OutOfMemoryError,StackOverflowError etc.

Difference between Error and Exception

Error:-

Error class present in java.lang package.

Error are mostly caused by environment in which application is running.

All errors in java are unchecked type.

Errors occurs at run time. They will not be known to compiler.

e.g. OutOfMemoryError,StackOverflowError

Exception:-

Exception class present in java.lang package.

Exception include both checked and unchecked type.

You can handle exception using try catch block.

Exceptions are caused by application itself.

e.g.IOExceptio,SQLException,NullPointerException,ClassCastException,

ArrayIndexOutOfBoundException etc.

Exception occurs due to following reasons:-

  1. Requested file does not exist in system.
  2. Invalid inputs
  3. When Java virtual machine(JVM) runs out of memory.
  4. Network fluctuation.

How to handle exception in Java:-

Java exception handling managed by using following keywords.

  • Try
  • Catch
  • Throw
  • Throws
  • Finally

 

Advantages of Exception Handling:-

It maintains normal flow of the application

Exception Handling

Types of Exceptions:-

Checked Exception:-

There are exceptions which are detected by the compiler at time of compilation.

Exception Handling Example:-

package com.javatechexpert;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;




public class Checkedexception {




       public static void main(String args[]) {       

             File file = new File("D://file.txt");

             FileReader fr = new FileReader(file);

          }

}

 

Output:-

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

            Unhandled exception type FileNotFoundException




            at com.javatechexpert.Checkedexception.main(Checkedexception.java:11)

 

 

Unchecked Exception:-

A runtime exception is called unchecked exception.

Exception Handling Example:-

package com.javatechexpert;

public class Uncheckedexception {




         public static void main(String args[]) {

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

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

          }

}

 

Output:-

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6

            at com.javatechexpert.Uncheckedexception.main(Uncheckedexception.java:7)

 

Difference between Checked and Unchecked Exception:-

Checked Exception:-

  1. The class that extends the throwable class.
  2. Require to handle at compile time.

e.g  IOEception, FileNotFound Eception, ParseException,Nosuch Method, SQLException.

Unchecked Exception:-

  1. The class that extends that Runtime Exception.
  2. Require to handle at Runtime.

e.g NullpointerException, IndexOutOfBound Except,illegalstateException,ClasscastException.

 

Related Articles:-

Exception Interview Questions and Answers

Reference:-

Exception

 

Leave a Reply

%d bloggers like this: