We can throw our own exceptions using the keyword throw.Here the Throwable's subclass is actually a subclass derived from the Exception class.
User defined Exception
• We can
throw our own exceptions using the keyword throw.
• The
syntax for throwing out own exception is
throw
new Throwable's subclass
• Here
the Throwable's subclass is actually a subclass derived from the Exception
class.
For
example -
throw new ArithmeticException();
Throwable's
subclass
• Let us
see a simple Java program which illustrates this concept.
Java Program[MyExceptDemo.java]
import
java.lang.Exception;
class
MyOwnException extends Exception
{
MyOwnException(String
msg)
{
super(msg);
}
}
class
MyExceptDemo
{
public
static void main (String args[])
{
int age;
age=15;
try
{
if(age<21)
throw
new MyOwnException("Your age is very less than the condition");
}
catch
(MyOwnException e)
{
System.out.println
("This is My Exception block");
System.out.println
(e.getMessage());
}
finally
{
System.out.println
("Finally block:End of the program");
}
}
}
Output
This is
My Exception block
Your age
is very less than the condition
Finally
block:End of the program
Program
Explanation
• In
above code, the age value is 15 and in the try block exception if the value is
less than 21. As soon as the exception is thrown the catch block gets executed.
Hence as an output we get the first message "This is My Exception
block". Then the control is transferred to the class
MyOwnException(defined at the top of the program). The message is set and it is
"Your age is very less than the condition". This message can then
printed by the catch block using the System.out.println statement by means of
e.message.
• At the
end the finally block gets executed.
Ex. 3.9.1: Write an exception
class for a time of day that can accept only 24 hour representation of clock
hours. Write a java program to input various formats of timings and throw
suitable error messages.
Sol.:
import
java.lang.Exception;
import
java.io.*;
import
java.util.*;
class
MyException extends Exception
{
MyException(String
msg)
{
super(msg);
}
}
class
Clock
{
private
int hour;
private
int minute;
public
void input() throws IOException
{
BufferedReader br=new BufferedReader (new
InputStreamReader(System.in));
System.out.println("\n
Enter the time in hh:mm format");
String
str=br.readLine();
StringTokenizer
tokn=new StringTokenizer(str,":");
String
h=tokn.nextToken();
String
m=tokn.nextToken();
hour=Integer.parseInt(h);
minute=
Integer.parseInt(m);
try
{
System.out.println("Hour:
"+hour);
if((hour
< 0) || (hour>24))
throw
new MyException("Fatal error: invalid hour");
}
catch(MyException
e)
{
System.out.println(e.getMessage());
}
try
{
System.out.println("Minute:
"+minute);
if((minute
<0) || (minute > 59))
throw
new MyException("Fatal error: invalid minute");
}
catch(MyException
e)
{
System.out.println(e.getMessage());
}
}
}
class
ClockDemo
{
public
static void main(String[] args) throws IOException
{
Clock
c=new Clock();
c.input();
}
}
Output(Run1)
Enter
the time in hh:mm format
25:80
Hour: 25
Fatal
error: invalid hour
Minute:
80
Fatal
error: invalid minute
Output(Run2)
Enter
the time in hh:mm format
10:70
Hour: 10
Minute:
70
Fatal
error: invalid minute
Output(Run3)
Enter
the time in hh:mm format
30:40
Hour: 30
Fatal
error: invalid hour
Minute:
40
Review
Question
Object Oriented Programming: Unit III: Exception Handling and Multithreading : Tag: : with Example Exception Handling Java Programs - User defined Exception
Object Oriented Programming
CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation