Class Exception in Java

Class Exception in Java

The class Exception and its subclasses are a form of throwable objects that indicates conditions that can be caught using a catch clause in the code.

This class forms the root of the exception hierarchy in Java. An Exception signals an abnormal condition that must be specially handled to prevent program termination. Exceptions may be caught and handled. Exceptions that are not subclasses of RuntimeException must be declared in the throws clause of any method that can throw them.
getMessage() returns a message associated with the exception. See Throwable for other methods.

The Exception class is the superclass of all of the standard exception classes that can be thrown in Java. The subclasses of Exception represent exceptional conditions a normal Java program may want to handle. Any explicitly thrown object in a Java program should be an instance of a subclass of Exception.

Many of the standard exceptions are also subclasses of RuntimeException. Run-time exceptions represent run-time conditions that can occur generally in any Java method, so a method is not required to declare that it throws any of the run-time exceptions. However, if a method can throw any of the other standard exceptions, it must declare them in its throws clause.

A Java program should try to handle all of the standard exception classes since they represent routine abnormal conditions that should be anticipated and caught to prevent program termination.

See also  Learn Inheritance In Java Fast with Examples

The structure of the Exception class is given below:

public class java.lang.Exception extends java.lang.Throwable{
public Exception();
//constructs an empty Exception object that is an object with no message specified.
public Exception(String message);
//constructs an Exception object with the message specified.

This message is used later to extract information from the Exception.

The class Exception also inherits methods from class Object and Throwable.

From Object classFrom Throwable class
wait(long, int)getLocalizedMessage()

Share and Enjoy !

Leave a Reply

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