0% found this document useful (0 votes)
10 views4 pages

Java Exception Handling Examples

Uploaded by

sampath oruganti
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)
10 views4 pages

Java Exception Handling Examples

Uploaded by

sampath oruganti
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

2-BTECH-CSE:: I SEM

OOPS THROUGH JAVA PROGRAMMING LAB


EXPERIMENT: 6
6(a)AIM:: Write a JAVA program that describes exception handling mechanism
class Main {

public static void main(String[] args)

try {

// code that generate exception

int divideByZero = 5 / 0;

[Link]("Rest of code in try block");

catch (ArithmeticException e) {

[Link]("ArithmeticException => " + [Link]());

6(B): AIM: b) Write a JAVA program Illustrating Multiple catch clauses


public class MultipleCatchBlock1 {

public static void main(String[] args) {

try{

int a[]=new int[5];

a[5]=30/0;

catch(ArithmeticException e)

[Link]("Arithmetic Exception occurs");

catch(ArrayIndexOutOfBoundsException e)
{

[Link]("ArrayIndexOutOfBounds Exception occurs");

catch(Exception e)

[Link]("Parent Exception occurs");

[Link]("rest of the code");

} }

Output:
Arithmetic Exception occurs
rest of the code

6C:AIM: Write a JAVA program for creation of Java Built-in Exceptions


public class ExcepTest {

public static void main(String args[]) {

try {

int b = 0;

int c = 1/b;

[Link]("c :" + c);

catch (ArithmeticException e) {

[Link]("Exception thrown :" + e);

[Link]("Out of the block");

Output

Exception thrown :[Link]: / by zero

Out of the block


EX-2:
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
Can't divide a number by 0

6(D): Write a JAVA program for creation of User Defined Exception


class MyException extends Exception {

public MyException(String s)

// Call constructor of parent Exception

super(s);

} }

// A Class that uses above MyException

public class Main {

// Driver Program

public static void main(String args[])

try {

// Throw an object of user defined exception

throw new MyException("I CANT HANDLE THIS");

catch (MyException ex) {

[Link]("Caught");
// Print the message from MyException object

[Link]([Link]());

} } }

Output
Caught
I CANT HANDLE THIS

EX-2:

class EmployeeException extends Exception

{
public EmployeeException(String s)
{
super(s);
}
}
class SampleEmp
{
void empIDCheck(int EmpID) throws EmployeeException{
if(EmpID<=0 || EmpID>999){
throw new EmployeeException("Invalid Employee ID");
}
}
public static void main(String args[])
{
SampleEmp emp = new SampleEmp();
try
{
[Link](0);
}
catch (EmployeeException e)
{
[Link]("Exception caught");
[Link]([Link]());
}
}
}
Output:
Java User Defined Exception 2
If we are

You might also like