Sie sind auf Seite 1von 7

What is exception

Dictionary Meaning: Exception is an abnormal condition.

In java,
object

exception is an

event that disrupts the normal flow of the program. It is an

which is thrown at runtime.

Hierarchy of excpetion classes

What is exception handling


Exception Handling is a mechanism to handle runtime errors such as ClassNotFound,
IO, SQLArithematicException 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. Let's take a scenario:
1.
2.
3.
4.

statement
statement
statement
statement

1;
2;
3;
4;

5.
6.
7.
8.
9.
10.

statement
statement
statement
statement
statement
statement

5;//exception occurs
6;
7;
8;
9;
10;

Suppose there is 10 statements in your program and there occurs an


exception at statement 5, rest of the code will not be executed i.e.
statement 6 to 10 will not run. If we perform exception handling, rest of
the statement will be executed. That is why we use exception handling
in java.

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.

Based on these we have three categories of Exceptions you need


to understand them to know how exception handling works in
Java.
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 (handle)
these exceptions.

Unchecked exception
Unchecked exceptions are not checked at compile time. It means if your program is
throwing an unchecked exception and even if you didnt handle/declare that exception,
the program wont give a compilation error.

Difference between checked and unchecked exceptions


1) Checked Exception
The classes that extends 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.

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

Common scenarios where exceptions may occur


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

1) Scenario where ArithmeticException occurs


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

int a=50/0;//ArithmeticException

Lets understand this with an example:


2.

class Example {

3.

public static void main(String args[])

4.

5.

int num1=10;

6.

int num2=0;

7.

/*Since I'm dividing an integer with 0

8.

* it should throw ArithmeticException*/

9.

int res=num1/num2;

10.

System.out.println(res);

11.

12. }

If you compile this code, it would compile successfully however when you will run it,
it would throw ArithmeticException. That clearly shows that unchecked exceptions
are not checked at compile-time, they are being checked at runtime

2) Scenario where NullPointerException occurs


If we have null value in any variable, performing any operation by the variable occurs an
NullPointerException.
1.
2.

String s=null;
System.out.println(s.length());//NullPointerException

3) Scenario where NumberFormatException occurs


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.
1.
2.

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

4) Scenario where ArrayIndexOutOfBoundsException occurs


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

int a[]=new int[5];


a[10]=50; //ArrayIndexOutOfBoundsException

Lets see another example.


class Example {
public static void main(String args[])
{
int arr[] ={1,2,3,4,5};
/*My array has only 5 elements but
* I'm trying to display the value of

* 8th element. It should throw


* ArrayIndexOutOfBoundsException*/
System.out.println(arr[7]);
}
}

This code would also compile successfully sinceArrayIndexOutOfBoundsException is


also an unchecked exception.

class Example {
public static void main(String args[])
{
try{
int arr[] ={1,2,3,4,5};
System.out.println(arr[7]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("The specified index does not exist " +
"in array. Please correct the error.");
}
}
}

Java Exception Handling Keywords


There are 5 keywords used in java exception handling.
1.

try

2.

catch

3.

finally

4.

throw

5.

throws

Java try block


Java try block is used to enclose the code that might throw an exception. It must be used within
the method.
Java try block must be followed by either catch or finally block.

Syntax of java try-catch


1.
2.
3.

try{
//code that may throw exception
}catch(Exception_class_Name ref){}

Syntax of try-finally block


1.
2.
3.

try{
//code that may throw exception
}finally{}

Java catch block


Java catch block is used to handle the Exception. It must be used after the try block only.
You can use multiple catch block with a single try.

Problem without exception handling


Let's try to understand the problem if we don't use try-catch block.
1.
2.
3.
4.
5.
6.

public class Testtrycatch1{


public static void main(String args[]){
int data=50/0;//may throw exception
System.out.println("rest of the code...");
}
}

Das könnte Ihnen auch gefallen