User Defined Exception: Compiled By: Aneeta Siddiqui
User Defined Exception: Compiled By: Aneeta Siddiqui
Lecture # 14
1
Course Books
Text Books:
Cay S. Horstmann, Big Java: Early Objects, Wiley, 7th Edition
Herbert Schildt, Java: A Beginner's Guide, McGraw-Hill Education,
Eighth Edition
2
Course Instructors
3
The Exception Class Hierarchy
Classes that define exceptions are related by
inheritance, forming an exception class hierarchy
4
Checked Exceptions
An exception is either checked or unchecked
10-5
Unchecked Exceptions
An unchecked exception does not require
explicit handling, though it could be processed
that way
7
User-Defined Exceptions
Problem Statement :
Consider the example of the Circle class
Circle class had the following constructor
public Circle(double centreX, double centreY,
double radius){
x = centreX; y = centreY; r = radius;
}
8
Defining Your Own Exceptions
To define your own exception you must do the
following:
Create an exception class to hold the exception data.
Your exception class must subclass "Exception" or another
exception class
Note: to create unchecked exceptions, subclass the
RuntimeException class.
Minimally, your exception class should provide a constructor
which takes the exception description as its argument.
10 Exception class .
Example1:Defining your own
exceptions
import java.lang.Exception;
class InvalidRadiusException extends Exception {
private double r;
11
Throwing the exception
class Circle {
double x, y, r;
class CircleTest {
public static void main(String[] args){
try{
Circle c1 = new Circle(10, 10, -1);
System.out.println("Circle created");
}
catch(InvalidRadiusException e)
{
e.printError();
}
}
}
13
Example 2: Defining your own
exceptions
public class DivException extends Exception {
DivException()
{super(“Div error Exception…");} Exception have two constructors
String argument
Zero argument
DivException(String str)
{super(str);}
User will throw this exception if divisor is 0
@override
public String getMessage(){
return super.getMessage();
}
}
14
Example 2: Defining your own
exceptions
public class MyException extends Exception {
MyException()
{super("a<b in my Exception…");}
MyException(String str)
{super(str);} User will throw this exception if dividend <divisor
A=5
B=10
A<B
@Override
public String getMessage(){
return super.getMessage();
}
}
15
Throwing & Catching the exception
public class testMyException {
finally{
public static void main(String[] args){
System.out.println("Close File");
int a=10,b=20;
System.out.println("pakistan");
}
try{
System.out.println("hello Karachi");
if(a<b) throw new MyException();
}
divide(a,b);
static void divide(int i,int j) throws
} DivException{
catch(MyException e ){ if(j==0) throw new DivException();
System.out.println("MyException System.out.println(""+i/j);
Comes");
System.out.println(""+e.toString()); }
catch(DivException e ){
}
System.out.println("DivException
Comes"); }
System.out.println(""+e.toString()); }
16
Example 2: output
If a=10 b=20
pakistan
MyException COmes
exception.MyException: a<b in my Exception…
Close File
hello Karachi
If a=20 b=0
pakistan
DivException COmes
exception.DivException
Close File
hello Karachi
17