What is an Exception in Java

An exception in java is any abnormal condition arising during the execution of the program. An Exception is an unwanted event that interrupts the normal flow of the program. When an exception occurs program execution gets terminated. In such cases we get a system generated error message.

Java Exceptions Class

The exception class is the class that deals with the exceptions that occur while the program is running.

Types of Exception in Java

1. Checked Exception in Java

Checked Exception which is also known as compile-time exceptions.

Checked Exceptions are those exceptions that the compiler checks at compile time. If there is a checked exception inside a method then the method should handle the exception or it may mention the exception by using throws keyword.

Examples of checked Exceptions

  • IOException
  • SQLException
  • FileNotFoundException
  • InvocationCountException
  • ClassNotFoundException

2. Unchecked Exception in Java

Java Unchecked Exception which is also known as Runtime Exceptions.

Unchecked exceptions are those exceptions that occur at runtime. It completely depends on the programmer whether he or she wants to handle the exception or not. The exceptions under the Error class and the Runtime Exception class are unchecked exceptions.

Examples of Unchecked Exceptions

  • ArithmeticException,
  • NullPointerException,
  • ArrayIndexOutOfBoundsException,
  • StringIndexOutOfBoundsException
  • NumberFormatException
  • IllegalArgumentException

How to Handle Exception in java

The process of dealing with exceptions is known as Exception Handling.

Errors and exceptions are totally different things. An error occurs when we miss a semicolon at the end of the line or misspell a method name. We cannot handle an error.

However, Exceptions are manageable. We can easily handle an exception and ask the compiler to perform specific tasks when the program encounters an error.

All errors and exceptions come under the throwable class in Java. The throwable class has two subclasses:

a. Exceptions

b. Error

Exception handling in Java comprises of “try”, “catch” and “finally” blocks.

try Catch Blocks

The try block in Java contains the statements where there is a chance of exception.

We can’t write try block only. We always to write catch block.

A catch block is a block where we can handle the exceptions. The catch block must always follow the try block. There can be multiple catch blocks in a try block.

We can write try block Without catch block. But we need to write finally block

Syntax of try catch in Java

try {

//code where exceptions may occur

}

catch (Exception e) {

//Code to handle the exception

}

The control flow is as follows:

The code inside the try block gets executed first. If any exception occurs, then the control immediately shifts to the catch block. If there are statements after the line in which the exception occurs, then these statements are not executed.

After the exception gets passed as an Object of type Exception, the catch block statements get executed.

Multiple Catch Blocks in Java

We can place multiple catch blocks to one try block. If there are multiple exception classes in the try block, then Java allows several catch blocks to handle them.

The syntax of multiple catch block is as follows:

try {

//Code where exception can occur

}

Catch (ExceptionTypeA e1) {

//code that executes if Exception of type A occurs

}

Catch (ExceptionTypeB e2) {

//code that executes if Exception of type B occurs.

}

The control flow transfers from the try block immediately to the first exception. If it matches the exception type mentioned in the condition, it executes the code, else it falls down the exception ladder.

finally block

The “finally” block follows the try block or the catch block. This segment houses the code which gets executed whether an exception occurs or not. This may contain a cleanup code that we want to execute after the protected block.

Only one finally block is used for one try block.

Finally block is used to close the objects, connections to flush the IO streams etc.

The syntax is as follows:

try {

//Code where exception can occur

}

Catch (Exception e) {

// Code to handle the exception}

finally {

//cleanup code

}

Finally block will not be executed in 3 conditions

System. exit (0)

Exception in finally block: The line at which exception occurs from that line, finally block won’t be execute.

Death of thread

Advantages of Handling Exception in Java

It helps to separate error-handling code from the regular code. This means that the code while debugging it is easy to identify the code which handles the error.

It clarifies the code by removing error-handling code from the mainline of the program

Handling Exception in java helps in ordering and grouping the code for execution upon the occurrence of an exception.

Error

Error is not an exception but it is an issue that emerges out of the control of the user or the developer. We generally overlook the Errors in Java since we seldom take care of a mistake. For example, if there is stack overflow, there will be blunders.

Examples of errors: stack overflow, VirtualMachineError, AssertionError

There are two types of errors:

1. Compile-time errors in Java

Compile-time errors are the errors resulting from a violation of programming language’s grammar rules e.g., writing syntactically incorrect statements like

System.out.println “A Test”;

Will result in a compile-type error because of invalid syntax. All syntax errors are reported during compilation.

2. Run-time errors in Java

Runtime errors occur during runtime or execution of the program because of unexpected situations. We use Exception handling routines of Java to handle such errors.

Some common examples of errors are:

  • Divide by zero errors
  • Accessing the array elements beyond the range.
  • Invalid input
  • Hard disk crash
  • Opening a non-existent file
  • Heap memory exhausted
 Other Popular Articles

What is Java Collections?

Leave a Comment