JAVA: Exception Handling

Diwakar pratap
3 min readMar 22, 2022

An exception is AN unwanted or surprising event, that happens throughout the execution of a program i.e. at run time, that disrupts the conventional flow of the program’s directions.

Reasons to occur exception

· Invalid user input
· Device failure
· Loss of network affiliation
· Physical limitations (out of disk memory)
· Code errors
· Opening AN unprocurable file

Error

· Errors represent forgotten conditions like Java virtual machine (JVM) running out of memory, memory leaks, stack overflow errors, library incompatibility, infinite formula, etc.
· Errors square measure typically on the far side the management of the coder and that we mustn’t try and handle errors.

Error vs Exception

· Error: a blunder indicates a significant downside that an inexpensive application mustn’t try and catch.
· Exception: Exception indicates conditions that an inexpensive application may try and catch.

Exceptions can be Categorized into 2 Ways:

1. Built-in Exceptions
Built-in exceptions are the exceptions that are obtainable in Java libraries. These exceptions are appropriate to clarify sure error things.

i. Checked Exceptions
Checked exceptions are referred to as compile-time exceptions as a result of these exceptions are checked at compile-time by the compiler.

ii. Unchecked Exceptions
The unchecked exceptions are simply opposite to the checked exceptions. The compiler won’t check these exceptions at compile time. In easy words, if a program throws associate degree unchecked exception, and even though we have a tendency to didn’t handle or declare it, the program wouldn’t provides a compilation error.

2. User-Defined Exceptions
generally, the intrinsic exceptions in Java aren’t able to describe a particular state of affairs. In such cases, users can even produce exceptions that ar referred to as ‘user-defined Exceptions’.

Hierarchy of Java Exception categories

The java.lang.Throwable category is that the root category of Java Exception hierarchy hereditary by 2 subclasses: Exception and Error. The hierarchy of Java Exception categories is given below:

Keywords

· Try
· Catch
· Finally
· Throw
· Thorws

1. Try
The “try” keyword is employed to specify a block wherever we should always place associate degree exception code. It suggests that we won’t use strive block alone. The strive block should be followed by either catch or finally.

2. Catch
The “catch” block is employed to handle the exception. It should be preceded by strive block which implies we won’t use catch block alone. It may be followed by finally block later.

3. Finally
The “finally” block is employed to execute the mandatory code of the program. it’s dead whether or not associate degree exception is handled or not.

4. Throw
The “throw” keyword is employed to throw associate degree exception.

5. Throws
The “throws” keyword is employed to declare exceptions. It specifies that there could occur associate degree exception within the methodology. It does not throw associate degree exception. it’s forever used with methodology signature.

Example

// WAP to read two input from user and handle divide by zero error

package twentyOne;
import java.util.*;
import java.io.*;
public class classassignment01 {public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
try
{
System.out.print(“Enter the 1st number: “);
int n=sc.nextInt();
System.out.print(“Enter the 2nd number: “);
int m=sc.nextInt();
float res=n/m;
System.out.printf(“Division of two number is: %f”,res);
}
catch(Exception e)
{
System.out.printf(“The Exception occured is %s”,e.toString());
}
}
}

Sample output

Enter the 1st number: 1
Enter the 2nd number: 0
The Exception occured is java.lang.ArithmeticException: / by zero

--

--