Custom Exception In Java
Creating Custom Exception In Java

Create Custom Exception In Java

Friends today we are discussing about the concept of creating custom exception in java programming. Exceptions are undesirable interrupt which are come in between the execution of programmer. Now we can start , so friend there is a question before reading this concept “What is the need of creating custom exception? “ So friend answer of this question is that there is many type of exception which are not defined in hierarchy of exception which are predefined like AirthmeticException , IOException . In real life application development and programming there is situation may arise where we have to need to create custom exception in java so this very important concept.
To create your own exception we have follow these point
i) All exception must be child of Throwable.
ii) To write a checked exception you will need to extend Exception class.
iii) To write a runtime exception you will need to extend RuntimeException class.
. Here is an example where we create custom Exception.
Now Before start programming we have to check following condition
1) Is jdk is installed on your pc?
2) Is path variable is set for programming in java?
Now we can start
Example Code
import java.util.*;
import java.io.*;

class Agexception extends Exception
{
int age;
Agexception(int age)
{
this.age=age;
}
}
class ag
{int y;
static void checkage(int a) throws Agexception
{
if (a= 18″);
}
}
public static void main(String args[])
{
int x;
try
{
System.out.println(“Enter your age”);
Scanner s=new Scanner(System.in);
x=s.nextInt();

checkage(x);
}
catch(Agexception agee)
{
System.out.println(“Exception handled”);
}
}
}
This Example is created to check whether person is of age 18 or not.
There are many situation like balance checker where we have to made our custom exception.
Custom exception are very important concept in real life application development technique.

:::::::::::::::::Output::::::::::::::::

Creating Custom Exception In Java
Creating Custom Exception In Java

NO COMMENTS