Login
Register

Home

Trainings

Fusion Blog

EBS Blog

Authors

CONTACT US

Fusion Blog
  • Register

Oracle Gold Partners, our very popular training packages, training schedule is listed here
Designed by Five Star Rated Oracle Press Authors & Oracle ACE's.

webinar new

Search Courses

Objective: In the previous article Interface in Java we have learned about the Interfaces, Extending Interface and Extending Multiple Interface. In this article we will learn about the Exception in Java.

 Exceptions:

In java, exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.

There are two types of Exceptions in Java:

  1. Checked Exceptions

  2. Unchecked Exceptions

  3. Error

1. Checked exception: The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions.     

e.g.IOException, SQLException etc. Checked exceptions are checked at compile-time.

2. Unchecked Exception: The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime.

There are given some scenarios where unchecked exceptions can occur. They are as follows:

 ->Arithmetic Expression:

If we divide any number by zero, there occurs an ArithmeticException.

int a=50/0;//ArithmeticException  

>Null Pointer Exception:

If we have null value in any variable, performing any operation by the variable occurs an ->NullPointerException

String s=null;  

System.out.println(s.length());//NullPointerException

->Number format Exception:

The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur ->NumberFormatException.

String s="abc";  

int i=Integer.parseInt(s);//NumberFormatException  

->ArrayINdexOutOfBound Exception:

If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as shown below:

int a[]=new int[5];  

a[10]=50; //ArrayIndexOutOfBoundsException  

3. Error: Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.

Exception Handling in Java:

There are 5 keywords used in java exception handling.

  1. try

  2. catch

  3. finally

  4. throw

  5. throws

g1.png

Exception Handling:

Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc.

The core advantage of exception handling is to maintain the normal flow of the application. Exception normally disrupts the normal flow of the application that is why we use exception handling.

Example:

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);
  }
  }

If you try to compile the above program you will get exceptions as shown below.

C:\>javac FilenotFound_Demo.java
FilenotFound_Demo.java:8: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
     FileReader fr = new FileReader(file);
                     ^

1 error

Note: Since the methods read() and close() of FileReader class throws IOException, you can observe that compiler notifies to handle IOException, along with FileNotFoundException.

public class Unchecked_Demo {

public static void main(String args[ ])

{

     int num[]={1,2,3,4};
     System.out.println(num[5]);
  }

}

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

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Exceptions.Unchecked_Demo.main(Unchecked_Demo.java:8)

 Catching Exception:

A method catches an exception using a combination of the try and catchkeywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following:

try
{
  //Protected code
}catch(ExceptionName e1)
{
  //Catch block
}

The code which is prone to exceptions is placed in the try block, when an exception occurs, that exception occurred is handled by catch block associated with it. Every try block should be immediately followed either by a class block or finally block.

A catch statement involves declaring the type of exception you are trying to catch. If an exception occurs in protected code, the catch block (or blocks) that follows the try is checked. If the type of exception that occurred is listed in a catch block, the exception is passed to the catch block much as an argument is passed into a method parameter.


Varun Kapila

Add comment


Security code
Refresh

About the Author

Varun Kapila

Search Trainings

Fully verifiable testimonials

Apps2Fusion - Event List

<<  Apr 2024  >>
 Mon  Tue  Wed  Thu  Fri  Sat  Sun 
  1  2  3  4  5  6  7
  8  91011121314
15161718192021
22232425262728
2930     

Enquire For Training

Fusion Training Packages

Get Email Updates


Powered by Google FeedBurner