Share this post on:

Custom Exception in Java is also called as user defined Exception. In this Exception class can be created by user itself. Exception message, Exception generate, Exception handle, all things are created and manage by the user or by the developer. we have to create one exception class just extending its by predefined generalize exception class.

Syntax:

class classname extends Exception

{

}

Example : Write a Java Program to demonstrate user defined Exception

class UserDefineException extends Exception
{
public UserDefineException()
{
super(“Invalid Age Exception”);
}
}
public class TestDemo
{
public static void main(String[] args)
{
if(args.length ==0)
System.out.println(“Age”);
else
{
try
{
int age = Integer.parseInt(args[0]);
if(age<18 || age>60)
{
throw new UserDefineException();
}
else
{
System.out.println(“Group of Party”);
}
}
catch(UserDefineException e)
{
System.out.println(e);
}
}
System.out.println(“Normal Flow”);
}
}

Output:

Age
Normal Flow

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

Leave a Comment

Your email address will not be published. Required fields are marked *