0% found this document useful (0 votes)
6 views3 pages

Java Exception Handling Programs

The document provides Java programs demonstrating exception handling mechanisms, including a basic example of handling an ArrayIndexOutOfBoundsException, illustrating multiple catch clauses for different exceptions, and showcasing built-in exceptions like ArithmeticException. Each program includes a description of its aim and the expected output. The examples emphasize the importance of maintaining the normal flow of the application despite runtime errors.

Uploaded by

lavanya.d
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Java Exception Handling Programs

The document provides Java programs demonstrating exception handling mechanisms, including a basic example of handling an ArrayIndexOutOfBoundsException, illustrating multiple catch clauses for different exceptions, and showcasing built-in exceptions like ArithmeticException. Each program includes a description of its aim and the expected output. The examples emphasize the importance of maintaining the normal flow of the application despite runtime errors.

Uploaded by

lavanya.d
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

WEEK-6

a) Write a JAVA program that describes exception handling mechanisms.

Aim: Java program on Exception Handling.


Description:
Exception is an abnormal condition. In java, exception is an event that disrupts the normalflow
of the program. It is an object which is thrown at runtime.
Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO,SQL,
Remote etc. The core advantage of exception handling is to maintain the normal flow of the
application. Exception normally disrupts the normal flow of the application that is why we use
exception handling.

Program:
import [Link]. *;
public class ExcepTest
{
public static void main(String args[])
{
int a[] = new int[2];
try
{
[Link]("Access element three:" + a[3]);
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("Exception thrown:" + e);
}
Finally
{
a[0] = 6;
[Link]("First element value: " +a[0]);
[Link]("The finally statement is executed");
}
}
}
Output:
WEEK-6
b) Write a JAVA program illustrating Multiple catch clauses.
Aim: Illustrating multiple catch clauses
Description:
To perform different tasks at the occurrence of different Exceptions, use java multi
catch block. A single statement may throw more than one type of exception. In such cases, Java
allows you to put more than one catch blocks. One catch block handles one type of exception.
When an exception is thrown by the try block, all the catch blocks are examined in the order
they appear and one catch block which matches with exception thrown will be executed. After,
executing catch block, program control comes out of try- catch unit.

Program:
class ExceptionExample
{
public static void main(String argv[])
{
int num1 = 10;
int num2 = 0; int result = 0;
int arr[] = new int[5];
try
{
arr[0] = 0;
arr[1] = 1;
arr[2] = 2;
arr[3] = 3;
arr[4] = 4;
//arr[5] = 5;

result = num1 / num2;


[Link]("Result of Division : " + result);

}
catch(ArithmeticException e)
{
[Link]("Err: Divided by Zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("Err: Array Out of Bound");
}
}
}

Output:
WEEK-6

c) Write a JAVA program for the creation of Java Built-in Exceptions.


Aim: To illustrate built-in exceptions in exception handling
Program:
class ArithmeticException_Demo
{
public static void main(String args[])
{
try
{
int a = 30, b = 0;
int c = a / b; // cannot divide by zero [Link]("Result = " + c);
}
catch (ArithmeticException e)
{
[Link]("Can't divide a number by 0");
}
}
}
Output:

You might also like