×
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
5121    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
What makes one man great and another weak and low is shraddha (concentration).
Swami Vivekananda
3734
37.16
Today So Far
Total View (Lakh)
softetechnologies
01/01/2018     34102
28/06/2017     30587
26/05/2018     29418
02/08/2017     28490
25/06/2018     24453
15/05/2017     24273
01/08/2017     22969
06/07/2017     22777
21/04/2018     19043
14/07/2017     17966
softetechnologies