Exception Handling in Java

Exception Handling in Java, What is Exception handling, Types of exceptions in Java, Java Program Run-time Errors, and Java try-catch block.

Java Exception Handling

Dictionary Meaning: Exception is an abnormal condition.

What is an Exception in Java?

An exception is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally,

When an error occurs, Java will normally stop and generate an error message. The technical term for this is: Java will throw an exception.

An exception can occur for many different reasons:

i. A user has entered invalid data.

ii. A file that needs to be opened cannot be found.

iii. A network connection has been lost in the middle of communications.

Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner.

What is Exception Handling?

Exception Handling is a mechanism to handle runtime errors such as ArithmeticException, IOException, NullPointerException, RemoteException, etc.

Types of Java Exceptions:

1. Checked Exception
The classes which directly inherit Throwable class except RuntimeException and Error are known as checked exceptions e.g. IOException, SQLException, etc. Checked exceptions are checked at compile-time.

2. Unchecked Exception
The classes which inherit RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time, but they are checked at runtime.

3. Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.

Java Exception Keywords:

There are 5 keywords that are used in handling exceptions in Java.

1. try
2. catch
3. finally
4. throw
5. throws


Common Scenarios of Java Exceptions:

1. Scenario where ArithmeticException occurs.

If we divide any number by zero then ArithmeticException occurs.

Ex:
int a=10/0; //ArithmeticException

2. Scenario where NumberFormat Exception occurs.

The wrong format of any value

String s1=”abc”;
String s2=”123″;

int a= Integer.parseInt(s1);
int a= Integer.parseInt(s2);

System.out.println(a); //NumberFormatException

3. Scenario where NullPointerException occurs.

String s=null;
System.out.println(s.length()); //NullPointerException

4. Scenario where ArrayIndexOutOfBoundsException occurs

If we assign any value in the wrong index of an Array.

Ex:
int [] a= new int[3];
.
.
.
int a[7]=70;
System.out.println(a[7]); //ArrayIndexOutOfBoundsException

Examples:

1. A Java Program with Exception (Run-time Error):

int a=10;
int b=0;

int result=a/b;
System.out.println(result);
System.out.println(“Hello Java”);
System.out.println(“Hello Selenium”);

The above program throws an Exception (ArithemticException) and the program won’t be executed completely.

Exception handling: Use try-catch block to handle the run-time error.

Syntax for try-catch block:

try
{
Statements
…………..
…………..
…………..
catch (ExceptionName name)
{
exception handling code
}

2. Java Program with Exception handling code:

int a=10;
int b=0;

try
{
int result=a/b;
System.out.println(result);
}
catch (ArithmeticException e1)
{
System.out.println(“Divided by Zero Error”);
}
System.out.println(“Hello Java”);
System.out.println(“Hello Selenium”);

3. Handle multiple exceptions

int a=10;
int b=0;
int result;
int [] array1= new int[4];
try
{
result=a/b;
System.out.println(result);
}
catch (ArithmeticException e1)
{
System.out.println(“Divided by Zero Error”);
}
System.out.println(“Hello Selenium”);
try
{
array1[1]=100;
System.out.println(array1[10]);
}
catch (ArrayIndexOutOfBoundsException e2){
System.out.println(“Array Out of Bound Error”);
}
System.out.println(“Hello Java World”);


Java Tutorial

Java Videos

Follow me on social media:

1 thought on “Exception Handling in Java”

Comments are closed.