You are on page 1of 17

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

 Aneeta Siddiqui aarshad@ssuet.edu.pk


Assistant Professor, CED
Room Number: BS-03
Tel: 111-994-994,

3
The Exception Class Hierarchy
 Classes that define exceptions are related by
inheritance, forming an exception class hierarchy

 All error and exception classes are descendents of the


Throwable class

 A programmer can define an exception by extending the


Exception class or one of its descendants

 The parent class used depends on how the new


exception will be used

4
Checked Exceptions
 An exception is either checked or unchecked

 A checked exception either must be caught by a


method, or must be listed in the throws clause of
any method that may throw or propagate it

 A throws clause is appended to the method


header

 The compiler will issue an error if a checked


exception is not caught or asserted in a throws
clause

10-5
Unchecked Exceptions
 An unchecked exception does not require
explicit handling, though it could be processed
that way

 The only unchecked exceptions in Java are


objects of type RuntimeException or any of
its descendants
 Errors are similar to RuntimeException and
its descendants in that:
 Errors should not be caught

 Errors do not require a throws clause


10-6
Creating Custom Exception Classes

 Use the exception classes in the API whenever


possible.
 Create custom exception classes if the
predefined classes are not sufficient.
 Declare custom exception classes by extending
Exception or a subclass of Exception.

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;
}

 How would we ensure that the radius is not zero or


negative?

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.

To throw your own exceptions:


If your exception is checked, any method which is going to throw
the exception must define it using the throws keyword
When an exceptional condition occurs, create a new instance of
the exception and throw it.
User-Defined Exceptions in standard
format
class MyException extends Exception
{
MyException(String message)
{
super(message); // pass to superclass if parameter is not handled by used defined exception
}
}
class TestMyException {

try {
..
throw new MyException(“This is error message”);
}
catch(MyException e)
{
System.out.println(“Message is: “+e.getMessage());
}
}
}
Get Message is a method defined in a standard

10 Exception class .
Example1:Defining your own
exceptions
import java.lang.Exception;
class InvalidRadiusException extends Exception {

private double r;

public InvalidRadiusException(double radius){


r = radius;
}
public void printError(){
System.out.println("Radius [" + r + "] is not valid");
}
}

11
Throwing the exception
class Circle {
double x, y, r;

public Circle (double centreX, double centreY, double


radius ) throws InvalidRadiusException {
if (r <= 0 ) {
throw new InvalidRadiusException(radius);
}
else {
x = centreX ; y = centreY; r = radius;
}
}
}
12
Catching the exception

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

You might also like