An exception can be defined as “anything which interrupts the normal program flow”. The program processing will be stopped immediately, whenever there is an exception. Exception occurred during run-time are called as runtime exceptions and exception occurred during compile time are called as compile time exceptions.
Exception and Errors- Difference
Errors are complicated and crucial problems present in the machine or JVM. Errors are the problems which were not expected by the developer. Exceptions are the conditions that present in the code which can be rectified by the developer.
Why Exception Handling is Essential?
If the developer did not handle the raised exception, an unfriendly user message with compiler error is printed out as an output.
Exception Handling Advantages
- When exception is handled by the developer, it will allow the program to return to its normal flow.
- When calling method encounters an error, an exception will be thrown. So it allows the developer to handle the error in the calling the method.
- Using try-catch blocks, separate block codes can be differentiated from block codes which contains error.
Types of Exception Handling
There are many types of exception handling but exception handling such as Arithmetic Exception, NumberFormatException, ArrayIndexOutOfBounds Exception, StringIndexOutOfBound Exception and NullPointer Exception are the commonly used Exception handling.
Arithmetic Exception
Class: Java.Lang.ArithmeticException
The class mentioned above is a default class for arithmetic exception. The above mentioned class is stored in java.lang.package. Arithmetic exception will occur whenever an integer is divided by zero.
The following program is an example for arithmetic exception.
class ExceptioDemo
{
public static void main(String args[])
{
try
{
int nu1=20, nu2=00;
int op=nu1/nu2;
System.out.println (“Final Result = ” +op);
}
catch(ArithmeticException e1)
{
System.out.println (“Arithmetic Exception: You can’t divide an integer by zero”);
}
}
}
The output for the above program is: Arithmetic Exception: You can’t divide an integer by 00.
In the coding in the above program was to divide 20 by 00. Which is impossible so an arithmetic exception is thrown.
- SEE: Types of Binding
ArrayIndexOutOfBounds Exception
Class: Java.Lang.ArrayIndexOutOfBoundsException
When a referenced element is absent in an array, ArrayIndexOutOfBounds Exception will occur. For example 11 elements are mentioned in a 10 element array this exception will be thrown.
The program given below is an example for ArrayIndexOutOfBounds Exception
class ExceptioDemoTwo
{
public static void main(String args[])
{
try
{
int aa[]=new int[20];
//Array has only 20 elements
a1[22] = 19;
}
catch(ArrayIndOutOfBoundsException e1)
{
System.out.println (“ArrayIndOutOfBounds”);
}
}
}
The output for the program given above is: ArrayIndOutOfBounds.
After initializing the array with 20 elements, we initialized the array index with 22 elements so an ArrayIndOutOfBounds Exception is thrown.
StringIndOutOfBound Exception
Class: Java.Lang.StringIndOutOfBoundException
The exception is similar to ArrayIndOutOfBounds Exception. Whenever an index is enforced upon the string which has lower range, an object from above mentioned class is created.
The program given below is the example for StringIndOutOfBound Exception
class ExceptioDemoFour
{
public static void main(String args[])
{
try
{
String st1=”EasyStepsToBuildAWebsite”;
System.out.println(st1.length());;
char c1=st1.charAt(0);
c1=st1.charAt(30);
System.out.println(c1);
}
catch(StringIndOutOfBoundsException e1)
{
System.out.println(“StringIndOutOfBoundsException!!”);
}
}
}
The output for following program is StringIndOutofBoundException. This exception occurred because the referenced index is not a string.
NumberFormat Exception
Class: Java.Lang.NumberFormException
When a string is mentioned as a number, an object from above mentioned class is created. If you declare a string “ABC” as an integer, compiler will throw this exception.
The following program is an example for NumberFormat Exception:
class ExceptioDemoThree
{
public static void main(String args[])
{
try
{
int nu1=Inte.parseInt(“ABC”) ;
System.out.println(nu1);
}
catch(NumberFormException e1)
{
System.out.println(“The Number form kinda exception occurred”);
}
}
}
The output for this program is Number format exception occurred. In the above program the string “ABC” is declared as an integer which is why NumberForm Exception is thrown.
NullPointer Exception:
Class: Java.Lang.NullPointException
When a member is enforced with null object, an object from NullPointException will be created.
The program given below is an example for NullPointerException
package beginnersVook.com;
class Excepti2
{
public static void main(String args[])
{
try
{
String st1=null;
System.out.println (st1.length());
}catch(NullPointException e1)
{
System.out.println(“NullPointException..”);
}
}
}
Output for the above program is NullPointException. In the above program length() is defined as a function. The function should be used on an object but the object present in this program is null. So a NullPointException is thrown.