Java Exception Handling

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

Examples of some common exceptions include NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException, and ArithmeticException.

Exception:

An unintended event that has affected the program’s execution at run or compile time. Exceptions can be controlled using try, catch and finally block.

Try and 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.

Note: try block can be written without catch and finally block.

Syntax:

Common reasons for exceptions:

  1. Invalid user or program input
  2. disk out of memory
  3. Typing mistake while coding
  4. Network connection issue
  5. Resource(accessed file ) not found

Error:

This is an abnormal condition which was never thought of and occurred unexpectedly to stop programme execution. System crash and out of memory are the best example of error.

Dev team should never try to handle errors.

It is a subclass of throwable.

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:

Explanation: 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.

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

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

Example:

OUTPUT:

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 exception is thrown.

OUTPUT:

Throw

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

Example:

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.

OUTPUT:

Custom Exception:

Custom Exception is all about creating a user defined exception derived from Exception class. The class needs to extend from the Exception class to create a custom exception and override one argument constructor for create a meaning message.

Below is an example of creating a custom exception.

OUTPUT:

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan