Q. Write a program to create custom exception in java.
Answer:
Exception class is defined in Java library. But we can also create our own exception. This example of exception, shows how to create a custom exception and use it.
Output:

Answer:
Exception class is defined in Java library. But we can also create our own exception. This example of exception, shows how to create a custom exception and use it.
CustomException.java
public class CustomException extends Exception
{
public CustomException(String msg)
{
super(msg);
}
}
CustomDemo.java
public class CustomDemo
{
public static void main(String args[]) throws Exception
{
CustomDemo custom = new CustomDemo();
custom.display();
}
public void display() throws CustomException
{
for(int i=2;i<20;i=i+2)
{
System.out.println("i= "+i);
if(i==8)
{
throw new CustomException("My Exception Occurred");
}
}
}
}
Output:



