Lab 6: Exception Handling in Java
LAB OBJECTIVE: Gain ability to handle Exception that arises during the execution of a program.
Exception in Java
The term exception is shorthand for the phrase “exceptional event”. An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.
Exception Handling in Java
The Exception Handling in Java is one of the powerful mechanisms to handle runtime errors so that the normal flow of the application can be maintained.
// save as Exception.javac
/\*
Runtime Errors will occur
\*/
class Exception{
public static void main(String \[\]s){
int x=10/0; // divide by zero Exception
System.out.println(x);
System.out.println("this will not be printed");
}
}
How to Handle Exception In Java
All those lines which can generate some kind of exception at the run time must be written in a block called try{} followed by catch(){} block.
// save as Exception1.java
class Exception1 {
public static void main(String\[\] s) {
// keep the statements which generates some kind of exception
try {
// this lines causes the exception
int x = 10 / 0;
System.out.println(x);
}
// "catch" is used to catch the exception
catch (ArithmeticException e) {
System.out.println("exception handled");
}
System.out.println("this will be printed");
}
}
Output
javac Exception1.java
java Exception1
exception handled
this will be printed
Case 0: Another way to add try & catch block
//save as Exception2.java
class Exception2{
public static void main(String s\[\]){
try{
int x=10/s.length; // by default jvm will create zero length of string
System.out.println(x);
int z\[\]=new int\[s.length\];
System.out.println("Exception handled successfully");
}
catch(ArithmeticException e){
System.out.println(e);
}
}
}
Output
F:\\coreJAVA\\image>javac Exception2.java
F:\\coreJAVA\\image>java Exception2
java.lang.ArithmeticException: / by zero
F:\\coreJAVA\\image>java Exception2 hi Rst
5
Exception handled successfully
F:\\coreJAVA\\image>java Exception2 hi Rst 123
3
Exception handled successfully
Case 1: You can have multiple catch block with a single try block
//save as Exception3 .java
class Exception3 {
public static void main(String s\[\]) {
try {
int x = 10 / s.length;
System.out.println(x);
int z\[\] = new int\[s.length\];
z\[10\] = 100;
System.out.println(z);
} catch (ArithmeticException e) {
System.out.println(e);
}
System.out.println("Exception handled successfully");
}
}
// save as Exception4 .java
class Exception4 {
public static void main(String s\[\]) {
try {
int x = 10 / s.length;
System.out.println(x);
System.out.println("Exception handled successfully");
int z\[\] = new int\[s.length\];
// this cause ArrayIndexOutOfBoundsException
z\[10\] = 1000;
System.out.println(z);
}
// handler for ArithmeticException
catch (ArithmeticException e) {
System.out.println(e);
}
// handler for ArrayIndexOutOfBoundsException
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException handled successfully");
}
}
}
Output
(https://mgmt.rstforum.net/uploads/media-1701519203916.png)
Case 2: Single catch(){} block can handle multiple exception
// save as Exception6.java
class Exception6 {
public static void main(String s\[\]) {
try {
int x = 10 / s.length;
System.out.println(x);
System.out.println("Exception handled successfully");
int z\[\] = new int\[s.length\];
// this cause ArrayIndexOutOfBoundsException
z\[100\] = 1000;
System.out.println(z);
}
// Handling Multiple Exception
catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
System.out.println(e);
}catch (Throwable e) {
System.out.println(e);
}
}
}
Output
F:\\coreJAVA\\image>javac Exception6.java
F:\\coreJAVA\\image>java Exception6
java.lang.ArithmeticException: / by zero
F:\\coreJAVA\\image>java Exception6 hi vivek sir
3
Exception handled successfully
java.lang.ArrayIndexOutOfBoundsException: 100
Case 3: Nested try{}catch(){}
// save as Temp1.java
class Temp1{
public static void main(String s\[\]){
try{ // nested try block
try{
int x=10/s.length;
System.out.println(x);
}
catch(ArithmeticException e){
System.out.println(e);
}
try{
int z\[\]=new int\[s.length\];
z\[100\]=10;
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println(e);
}
}
catch(ArithmeticException e){}
}
}
/\*
Output:
F:\\coreJAVA\\11Exception Handling>javac Temp1.java
F:\\coreJAVA\\11Exception Handling>java Temp1 szhdf // command line arguments
10
java.lang.ArrayIndexOutOfBoundsException: 100
\*/
Case 4: Finally Block
//save as FinallyBlock.java
/\*
finally {} will execute whether exception caught or not \*/
class FinallyBlock {
public static void main(String args\[\]) {
try {
int data = 25 / 5;
System.out.println(data);
} catch (NullPointerException e) {
System.out.println(e);
}
//finally block will always execute
finally {
System.out.println("finally block is always executed");
}
System.out.println("rest of the code...");
}
}
/\*
Output:
F:\\coreJAVA\\11Exception Handling>javac FinallyBlock.java
F:\\coreJAVA\\11Exception Handling>java FinallyBlock
5
finally block is always executed
rest of the code...
\*/
Case 5: Exception occurs and not handled:
// save as FinallyBlock1.java
class FinallyBlock1 {
public static void main(String args\[\]) {
try {
int data = 25 / 0;
System.out.println(data);
} catch (NullPointerException e) {
System.out.println(e);
} finally {
System.out.println("finally block is always executed");
}
System.out.println("rest of the code...");
}
}
/\*
Output:
F:\\coreJAVA\\11Exception Handling>javac FinallyBlock1.java
F:\\coreJAVA\\11Exception Handling>java FinallyBlock1
finally block is always executed
Exception in thread "main" java.lang.ArithmeticException: / by zero
at FinallyBlock1.main(FinallyBlock1.java:5)
\*/
Case 6: Exception occurs and Handled
//save as FinallyBlock2.java
class FinallyBlock2 {
public static void main(String args\[\]) {
try {
int data = 25 / 0;
System.out.println(data);
} catch (ArithmeticException e) {
System.out.println(e);
} finally {
System.out.println("finally block is always executed");
}
System.out.println("rest of the code...");
}
}
Output
F:\\coreJAVA\\image>javac FinallyBlock2.java
F:\\coreJAVA\\image>java FinallyBlock2
java.lang.ArithmeticException: / by zero
finally block is always executed
rest of the code...
F:\\coreJAVA\\image>java FinallyBlock2 hi
java.lang.ArithmeticException: / by zero
finally block is always executed
rest of the code...