1.

Java Exception Handling

Answer»

Meaning of Exception: An exception is an unwelcome or unexpected occurrence that occurs during the execution of a program, i.e. at run time, and disturbs the program's usual flow of instructions.

Error vs Exception: What is the Difference?

An error implies that there is a major problem that a reasonable programme should not attempt to solve whereas an exception denotes a set of circumstances that a reasonable programme would attempt to catch.

Hierarchy of Exceptions:

As we can see in the above image, all exception and error kinds are subclasses of the hierarchy's root class, Throwable.  Exceptions can be divided into two categories:



  • Checked Exceptions: This includes IO Exceptions and Compile time Exceptions.


  • Unchecked Exceptions: This includes Runtime Exceptions and Null Pointer Exceptions.

Built-in Exceptions in Java: Exceptions that are present in Java libraries are known as built-in exceptions. The following table lists the built-in exceptions in Java:

ExceptionDescription
ArithmeticExceptionWhen an unusual condition in an arithmetic operation occurs, it is thrown.
ArrayIndexOutOfBoundsExceptionIt's thrown when an array has been accessed with an incorrect index. The index, in this case, is either negative, or greater than or equal to the array's size.
ClassNotFoundExceptionWhen we try to access a class whose definition is missing, this Exception is thrown.
FileNotFoundExceptionWhen a file is not accessible or does not open, this Exception is thrown.
IOExceptionWhen an input-output operation fails or is interrupted, this exception is issued.
InterruptedExceptionWhen a thread is interrupted while waiting, sleeping, or processing, this exception is issued.
NoSuchFieldExceptionIt's thrown when a class doesn't have the requested field (or variable).
NoSuchMethodExceptionIt's thrown when you try to call a method that doesn't exist.
NullPointerExceptionWhen referring to the members of a null object, this exception is thrown.
NumberFormatExceptionWhen a method fails to convert a string to a numeric representation, this exception is thrown.
RuntimeExceptionAny exception that happens during runtime is represented by this.
StringIndexOutOfBoundsExceptionIt's thrown by String class methods when an index is either negative or larger than the string's length.

Java Try-Catch:-


  • try block: The try block comprises a set of statements that may throw an exception.
Syntax -
try
{
// code to be checked
}

  • catch block: The catch block is used to manage the try block's unclear condition. A try block is always followed by a catch block, which deals with any exceptions thrown by the try block.
Syntax -
catch
{
// code to handle exception
}

finally keyword:-

In Java, the finally block represents code that is to be executed whether or not an exception is caught. In case a catch block is present after the try block, it is run after the catch block has been executed. However, in case there is no catch block present after the try block, the finally block is executed after the try block has been executed.

Syntax -

finally
{
// code to be executed
}

Difference between final, finally and finalize:

The following table lists the differences between final, finally and finalize:

finalfinallyfinalize
final is a keyword and access modifier for restricting access to a class, method, or variablefinally represents a block of code to be executed whether or not an exception has been caught.finalize is a Java method that performs cleanup operations immediately before an object is garbage collected.
The final keyword is used with the classes, methods and variables.The finally block is always linked to the try and catch block in exception handling. The finalize() method is used with the objects.
A final method is executed only when we call itAs soon as the try-catch block is finished, the finally block is started.It doesn't rely on exceptions for execution.

throw keyword:- In Java, the throw keyword is used to throw an exception from a method or any block of code. We can either throw a checked or an unchecked exception. Throwing custom exceptions is the most common use of the throw keyword.

throws keyword:- The throws keyword is used to handle exceptions in the absence of a try/catch block. It specifies the exceptions that a method should throw in case an exception occurs.

Example -

class Test {

// This method throws an exception
static void foo() throws IllegalAccessException
{
System.out.println("Inside the foo() method");
throw new IllegalAccessException("demo");
}

public static void main(String args[])
{
try {
foo();
}
catch (IllegalAccessException e) {
System.out.println("Exception caught");
}
}
}

Output -

Inside the foo() method
Exception caught

Explanation - In the above code, the function foo() specifies that an IllegalAccessException should be thrown in case an exception occurs using the throws keyword. In the foo() function, we explicitly throw an exception. This is caught by the catch block present in the main method of the class.




Discussion

No Comment Found