0% found this document useful (0 votes)
11 views2 pages

Java Exception Handling Examples

Uploaded by

duceog1718
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)
11 views2 pages

Java Exception Handling Examples

Uploaded by

duceog1718
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

WEEK 6

AIM:
1. [Link] a java program to illustrate usage of try, catch, finally with multiple exceptions

import [Link].*;
public class MultipleExceptions {
public static void main(String[] args) {
int a,b,c;
try {
try
{
a=0;
b=10;
c=b/a;
[Link]("This line will not be executed");
}
catch(ArithmeticException e)
{
[Link]("Divided by zero");
}
String firstArg = args[0];
[Link]("First Argument: " + firstArg);
}

catch (IndexOutOfBoundsException ex) {


[Link]("There is no argument");
}

finally {
[Link]("Finally gets executed");
}
}
}
OUTPUT:
Divided by zero
There is no argument
Finally gets executed
[Link] a program to create user defined exceptions

import [Link].*;
import [Link].*;
class MyException extends Exception{
String str1;
MyException(String str2) {
str1=str2;
}
public String toString(){
return ("Output String = "+str1) ;
}
}
public class UserDfExc{
public static void main(String args[]){
try{
throw new MyException("userdefiend exception");
}
catch(MyException e){
[Link]("Hi this is my catch block") ;
[Link](e) ;
}
}
}

OUTPUT:
Hi this is my catch block
Output String = user defiend exception

You might also like