Exception Handling

The exception handing is a powerful feature of Java. Exception handling allows us to handle any exception which occurs at runtime and stop to break the normal flow of a program.

When an Exception Occurs:

There can be various reasons which cause our application to throw exception. For example, open a file which doesn’t exist, connecting to a server, accessing an array with no existence index, dividing any number by zero.

OUTPUT:

In the above example an exception(java.lang.ArithmeticException) got occur at line number 5 on run time which broke our normal flow of program and because of that the line number 6 and 7 didn’t get execute.

How to handle above Exception:

OUTPUT:

Note: As we can see in above example, the normal flow of a program didn’t get break after handling an exception. For handling an exception, we covered line number 7 with try catch block.

Try and catch block:

Syntax:

try {
// Statement which might throw an exception.
} catch(Exception e) {

}

try cannot be written without catch block. In try block we write code which may throw an exception. If any exception occurs at particular line inside try block, the rest of the code after that line inside that try block will not get execute.

Catch will get call only at the time of exception occurs. If catch block is not able handle exception, it will throw this to caller function. Catching a wrong exception will also break the normal flow of a program.

Type of Exceptions:

Checked Exception

Checked Exception (Compile time exception) -> The checked exception is thrown at compile time by the compiler.

Example:

OUTPUT:

NOTE: As mentioned the checked or compile time exception will get thrown at the time of code compilation.

Unchecked Exception

Unchecked Exception (Runtime exception) -> The unchecked exception is thrown at run time while execution of program.

Example:

OUTPUT:

NOTE: The above program got compiled successfully. It thrown an exception on run time after running command (java MyException) to execute java program.

Finally

Finally block is used to execute for closing an open connections.

Note:

  1. finally will always get call either exception is thrown or not.
  2. finally block can be used either with catch or finally.
  3. finally block can be used with or without catch block.

Example:

In the below example finally will called just after excpetion is thrown.

public class MyException {
static int a, b = 10;
public static void main(String[] args) {
try {
a = b / 0;
} catch (ArithmeticException e) {
System.out.println("ArithmeticException occurs");
} finally {
System.out.println("Inside Finally");
}
}
}

OUTPUT:

Throws

Throws is a java keyword which indicate that this method can throw this type of exception and caller function need to catch the same exception.

Example:

Below function myfunc() will throw an Exception(ArithmeticException) which will get catch by caller main() function.

public class MyException {
static int a, b = 10;
public static void myFunc() throws ArithmeticException{
a = b / 0;
}

public static void main(String[] args) {
try {
myFunc();
} catch (ArithmeticException e) {
System.out.println("ArithmeticException occurs");
}
}
}

OUTPUT:

Throw

Throws keyword is provide us to throw an exception explicitly form a method, constructor or block of code.

Example:

public class MyException {
public static void main(String[] args) {
try {
throw new ArithmeticException();
} catch (ArithmeticException e) {
System.out.println("ArithmeticException occurs");
}
}
}

OUTPUT:

Custom Exception:

Below is the example for creating a custom exception. Class need to extends form Exception class for creating a custom exception and override one argument constructor for create a meaning message.

class MyCustomException extends Exception {
public MyCustomException(String message) {
super(message);
}
}

public class MyException {
static int age = 15;
public static void main(String[] args) {
try {
if (age < 18) {
throw new MyCustomException("Age is below 18");
}
} catch (MyCustomException e) {
System.out.println("Exception : "+e);
System.out.println("Exception Message : "+e.getMessage());
}
}
}

OUTPUT:

Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.

0