×
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
3659    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
I never teach my pupils, I only provide the conditions in which they can learn.
Albert Einstein
2014
25.81
Today So Far
Total View (Lakh)
softetechnologies
01/01/2018     31584
28/06/2017     26610
02/08/2017     23329
26/05/2018     21526
15/05/2017     20222
06/07/2017     19001
01/08/2017     17464
21/04/2018     17235
01/07/2017     14866
25/04/2018     14109
softetechnologies