×
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
6479    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.
Albert Einstein
It is the supreme art of the teacher to awaken joy in creative expression and knowledge.
Albert Einstein
461
55.7
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     43021
01/01/2018     36304
25/06/2018     34795
28/06/2017     34409
02/08/2017     32758
01/08/2017     27280
06/07/2017     27080
15/05/2017     26678
14/07/2017     22287
21/04/2018     20937
softetechnologies