×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Numbers Program In Java Part 2 Java String Programs - Word Count in java
Custom Exception Handling in Java - Java
6632    Arnab De    13/09/2017

We can create a custom exception handling in java. For that, we have to inherit the Exception class of java library. Here we explain some program which describes the custom Exception. We must display a valid statement for the exception. For that, we create a Class which extends Exception class.

Write a program using java that handles an exception "Entry of negative age of a person". [WBUT MCA 2014]

softetechnologies

class AgeBelow0 extends Exception
{
private int age;
AgeBelow0(int a)
{
age = a;
}
public String toString()
{
return "Age can Not Be " + age;
}
}

class TestAgeBelow0
{
static void age(int a) throws AgeBelow0
{
System.out.println("Called age(" + a + ")");
if(a < 0)
throw new AgeBelow0(a);
System.out.println("Normal exit");
}
public static void main(String args[])
{
try
{
age(10);
age(-20);
age(20);
}
catch (AgeBelow0 e)
{
System.out.println("Caught " + e.toString());
}
}
}

softetechnologies

Output:

Called age(10)
Normal exit
Called age(-20)
Caught Age can Not Be -20

Numbers Program In Java Part 2 Java String Programs - Word Count in java
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Swami Vivekananda
Truth can be stated in a thousand different ways, yet each one can be true.
Swami Vivekananda
397
58.54
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     44007
01/01/2018     36616
25/06/2018     35926
28/06/2017     34655
02/08/2017     33154
01/08/2017     27625
06/07/2017     27343
15/05/2017     26991
14/07/2017     22634
21/04/2018     21243
softetechnologies