Exception in Java With Examples
Aim of this program:
To Use inheritance to create an exception super class called Exception A and exception sub class Exception B and Exception C, where Exception B inherits from Exception A and Exception C inherits from Exception B. Write a java program to demonstrate that the catch block for type Exception A catches exception of type Exception B and Exception C
Exception in java small Description before coding
Create a class ExceptionA which extends Exception class. Create ExceptionB and ExceptionC classes, such that ExceptionB inherits from ExceptionA and ExceptionC inherits from ExceptionB. The catch block for type ExceptionA
catches exception of type ExceptionB and ExceptionC
Source code |Types of exception in java
import java.io.*;
import java.util.*;
class ExceptionA extends Exception
{
ExceptionA(String message)
{
super(message);
}
}
class ExceptionB extends ExceptionA
{
ExceptionB(String message)
{
super(message);
}
public static void getExceptionB() throws Exception
{
throw new ExceptionB("Exception B");
}
}
class ExceptionC extends ExceptionB
{
ExceptionC(String message)
{
super(message);
}
public static void getExceptionC() throws Exception
{
throw new ExceptionC("ExceptionC");
}
}
class TestException
{
public static void main(String ar[]) throws Exception
{
try
{
ExceptionB.getExceptionB();
}
catch(ExceptionA ea)
{
System.out.println("In B");
ea.printStackTrace();
}
try
{
ExceptionC.getExceptionC();
}
catch(ExceptionA ea)
{
System.out.println("In C");
ea.printStackTrace();
}
}
}
Output:
FAQ:
What are the types of exception in Java?
These are the types of Exceptions in Java:
ArithmeticException
ArrayIndexOutOfBounsException
ClassNotFoundException
IOException
InterruptedException
NoSuchFieldException
NoSuchMethodException
NullPointerException
NumberFormatException
RuntimeException
StringIndexOutOfBoundsException
What is an exception in Java with example?
The exception is nothing but types of error that occur at the run time of the program. Example dividing any number with zero ( 23 / 0) #This arises exception.
What is the difference between error and exception?
In java, an error is nothing but a syntactic mistake in the program and it will occur at compile time. But an exception is an error that occurs at the run time of the program, for example: dividing number with zero.There are two types of exceptions i.e. Checked Exception and Unchecked Exception.
Recommended Post:
- How to compare dates in java|algorithm with source code
- Java roll dice 10000 times with algorithm and source code
- Write a Java program that displays the number of characters, lines, and words in a text
- Write a Java program that reads a file and displays the file on the screen with a line number before each line
- Write a Java program that reads a file name from the user, then displays information about whether the file exists, readable, writable, type of file, and the length of the file in bytes
- Java program to make frequency count of vowels, consonants, special symbols, digits, words in a given text
- Write a Java program for sorting a given list of names in ascending order
- Write a java program to Checks whether a given string is a palindrome or not
- Write a java program to perform multiplication of two matrices
- write a java program that prints the Fibonacci series for a given number.
- Write a Java program that finds the factorial of a number
- Write a Java program that finds prime numbers between 1 to n
- Write a Java program that prints all real and imaginary solutions to the quadratic equation
- Odd and Even number in java | Algorithm
- Even number in java | algorithm with source code
- Java greater number using a loop
- Java Area of Rectangle
- What is a polygon? | Area of Triangle in java
- To calculate the Area of Circle in the java program
- Java Addition through user input
- Addition program in java with source code
Find the solution to the salesforce Question and many more