Java Exception Handling

October 27, 2018 Off By ravi24kant

Java Exception Handling

Exceptions are run time errors and Java Exception handling is to handle these errors, Exception is a situation that stops the program while you are running. As much memory as your program needs to be executed, so much memory will not stop if your computer does not have the execution of your program. This is out of memory exception. In such situations, you are called exceptions.

Just as if you forget to put semicolon in front of a statement when you create a program, then you show program error and do not compile. In the same way, when the exception is generated, your program stops and does not execute. If you want your program exception to remain on execution, then you have to handle the exceptions. This is called exception handling only. When you  handle the exceptions, your program does not stop on the exception, but skip the code with the exception and executes the rest of the code.

Java Exception Handling Keywords

In Java, you handle exceptions with the help of some keywords. All these
keywords together form a structure that is very easy to implement. These
keywords are being given below.

Try

The code of your program that can generate an exception, you write it in try block. For example, if you are doing a mathematical operation in the program and you think that an exception can be generated then you write that code in try block.

catch

If an exception is generated in the try block, it is  handled in this block. You write that code in this  block which you want to execute on an exception. For example, you can print a message that tells the user that an exception is generated.

throw

Most potential exceptions already define in java  library for you and this exception automatically java.But if you want you can also make  exceptions of yourself. You have to throw such exclusions yourself, for that you use the throw keyword. You can throw prefined exception even if you want.

Throws

You can use nested try blocks. If you want to have  an exception to the external handle block handle,
then in such situations, you use the throws  keyword.  You write the names of all the exceptions  by putting the keyword throws next to the definition. If there is an exception by doing this  then  the outer try block handles it.

Finally

After handling Exception, the code that you want to execute, finally writes in block. After the exception to the try block, the compiler does not execute that code and after the block of blockit executes the block  directly.

Steps to Handle Exceptions

  1. First of all, write that code in try block so that exception can be
    generated.
  2. After the try block, write the code to handle the exception in the catch
    block. As you can print a message related to the exception.
  3. After handling Exception, you want to execute the code that you want to
    execute in the block.

Some Common Exceptions

Before handling exceptions, it is very important for you to know about the
exceptions. There are some exceptions that come regularly in java. On these
common exceptions, you can easily practice and improve your skills. Some
common exceptions coming in Java are being given below.

Arithmetic Exception

Any arithmetic error such as if you try to divide a number by zero or try to
store a value greater than its size in a variable, Arithmetic-Exception
generates.

public class ArithmeticExceptionUnhandled
{
public static void main(String args[])
{
int a = 10, b = 0, c;
c = a/b;
System.out.println(c);
}
}

This above Java Class throw Arithmetic-Exception.

Class Cast Exception

If you want to store a class reference in the second class and if the first class
is not a sub class of the second class then Class-Cast-Exception is generated.

class Community{ }
public class Flat extends Community
{
public static void main(String args[])
{
Community c= new Community();;
Flat r = new Flat();

r = (Flat) f;
}
}

This above Java Class throw Class-Cast-Exception

Array Store Exception

If you have created a string array and you try to store the Integer in it then
Array-Store-Exception generates.

public class ArrayStoreException {

public static void main(String… args) {
String[] value = new Integer[4];
value[0] = 5.8;

}

}

This above Java Class throw ArrayStoreException

Array Index Out Of Bounds Exception

If the size of your array is 10 and you try to store the value at 11th position
or try to access 11th position, then Array-Index-Out-Of-Bounds-Exception
generates.

import java.util.ArrayList;
public class ArrayIndexException
{
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<>();
list.add(“My”);
list.add(“Name”);
System.out.println(list.get(2));
}
}

This above Java Class throw Array-Index-Out-Of-Bounds-Exception

Illegal Argument Exception

When you pass an invalid argument in a method such as passing the string
instead of int, Illegal-Argument-Exception generates.

public void checklegalParameter(String m) throws IllegalArgumentException {
if (m.instanceof(String) {
System.out.println(“m is a String”);
} else {
throw new IllegalArgumentException(“Illegal Parameter “);
}
}

Null Pointer Exception

In Java, you can assign a null value to a reference variable, but if you try to
use this reference variable, then NullPointerException is generated.

Number Format Exception

When you try to cast a string value in a number, then Number-FormatException
appears