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

Ganpat University: Akshi Vasaniya

The document contains 4 code examples that demonstrate exception handling in Java: 1. A program that catches an ArithmeticException from dividing by zero using a try-catch block. 2. A program that catches multiple exceptions (ArrayIndexOutOfBounds, NumberFormat, NullPointer) using multiple catch blocks and includes a finally block. 3. A program that defines a custom exception class extending Exception and throws an object of that type to demonstrate throwing a user-defined exception. 4. A program that uses the throws keyword in the main method signature to declare that the method may throw an InterruptedException.

Uploaded by

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

Ganpat University: Akshi Vasaniya

The document contains 4 code examples that demonstrate exception handling in Java: 1. A program that catches an ArithmeticException from dividing by zero using a try-catch block. 2. A program that catches multiple exceptions (ArrayIndexOutOfBounds, NumberFormat, NullPointer) using multiple catch blocks and includes a finally block. 3. A program that defines a custom exception class extending Exception and throws an object of that type to demonstrate throwing a user-defined exception. 4. A program that uses the throws keyword in the main method signature to declare that the method may throw an InterruptedException.

Uploaded by

divyansh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Akshi Vasaniya

Ganpat University
(Practical-8)

1. Write a program to catch ArithmeticException such as


division by zero.
Code –
public class Demo {

public static void main(String[] args) {

try {
func(5, 0);
} catch(ArithmeticException e){
System.out.println("Exception: "+e.getMessage());
}

static void func(int a, int b) {

int ans = a/b;

}
}

Output-

2. Write a program to catch multiple exceptions such as


ArrayIndexOutOfBounds Exception,
NumberFormatException, NullPointerException and also
use finally block.
Code –
public class Demo {

public static void main(String[] args) {

try {
func(null);
} catch(ArrayIndexOutOfBoundsException e){
System.out.println("Exception: "+e.getMessage());
} catch(NumberFormatException e) {
System.out.println("Exception :
"+e.getMessage());
} catch(NullPointerException e) {
System.out.println("Exception: "+e.getMessage());
}
finally {
System.out.println("Sorry for inconvenience");
}

static void func(String s) {

int[] x = {1, 2, 3, 5, 4};


System.out.println(x[6]);

int z = Integer.parseInt("null");

System.out.println(s.length());

}
}

Output –

3. Write a program to throw your own exception by


extending the Exception class.
Code –
public class BuildInException {

public static void main(String[] args) {

try {
throw new MyException("Exception fierd");
} catch(MyException e) {
System.out.println("Its my exception");

System.out.println(e.getMessage());
}

class MyException extends Exception{

public MyException(String s) {
super(s);
}

Output –

4. Write a program that shows the use of throws keyword.


Code –
public class Throws {

public static void main(String[] args) throws InterruptedException


{

Thread.sleep(5000);
System.out.println("Hello World");

Output –

You might also like