What do you mean by exception?
An exception is an abnormal condition that arises in a code sequence at run time. In simple words an exception is a runtime error. Java exception is one type of error in a condition that occurs in any block of code during execution. Exception handling is done in two ways
1. With Java runtime itself
2. With user’s code
Also read:-Memory allocation in java
Suppose you have made one program that divides one number by another number. But an exception occurs when we divide a particular number by zero. You all know it is not possible to divide any number by zero. So at that time we had to write the code of exception handling to handle Java run time error.
Let’s see what happen in the above situation and how Java run time handle the situation
Example of default exception handling in Java
Code
class exc1{
public static void main(String args[]){
int a=0;
int k=100/a;
System.out.println(“Will not execute”);
}
}
Exception message by JRE
java.lang.ArithmeticException: / by zero at exc1.main(Exc1.java:5)
Why this happened??
Because you all know that it is not possible to divide any number by zero. The program will compile easily and at runtime JRE automatically gives message to system of Arithmetic exception. There are some inbuilt exception in Java that are handled by the system itself..
Introduction to try, catch, and throw with basic guide and example
1. Try
Try block contains the statements that will lead to errors in run time.
The syntax is
try{
//statements
}
There must be try block in exception handling in Java
2. Catch
The catch block in exception handling is necessary as a pair of try block in Java. The catch block is used to catch the statements that lead to exception .Syntax of the catch is
catch(Exception object)
{
//statement
}
It is possible that one try block has multiple catch blocks. But there must be at least one catch block per try block.
3. Throw
It is used to throw an exception inside the try block. So that catch statement easily catches the exception in Java.
it can be declared by
try{
throw new exception(name);
}
Lets take a look at program of try-catch exception handling in java
Code
class exc2{
public static void main(String args[]){
int a=0;
try //Starting of try block
{
int k=100/a;
System.out.println(“Will not execute”); //This will not be executed
}
catch(ArithmeticException obj) //Starting of catch block
//Arithmetic exception type object
{
System.out.println(“Caught: “+obj); //will display exception defined by system
}
}
}
Output
Caught:java.lang.ArithmeticException: / by zero at exc1.main(Exc2.java:6)
Logic
Here inbuilt ArithmeticException is used by user inside try and catch block. The condition which leads to error at run time is written inside try block, whereas there is one catch statement which is able to catch ArithmeticException .We must have to declare the object of it. Note that if you not specify an exception type,then the system will find it default.
Example of a throw block in exception handling in Java
code
class exth1{
public static void main(String args[]){
try //try block
{
throw new ArithmeticException(“xyz”); //will throw arithmetic exception named xyz
}
catch(ArithmeticException e) //Will catch arithmetic exception
{
System.out.println(“There is : “+e); //Prints the result
}
}
}
Output
There is java.lang.ArithmeticException:xyz
Logic
There is throw block inside the try block. Here we have defined ArithmeticException using new operator. There is the catch statement which handles that exception. The output, which will you get after execution is there as above.
Also read :- Interface in java
So this is all about exception handling in Java. This tutorial will clear your basic concepts about exception handling in Java. The much more advanced guide about exception handling will be in the next tutorial. So stay connected.