×
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
4605    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
Education is not the learning of facts. It is rather the training of the mind to think
Albert Einstein
2013
32.39
Today So Far
Total View (Lakh)
softetechnologies
01/01/2018     33195
28/06/2017     29003
02/08/2017     26440
26/05/2018     25917
15/05/2017     22730
06/07/2017     21157
01/08/2017     20858
21/04/2018     18365
25/06/2018     17076
14/07/2017     15981
softetechnologies