Java.lang.Throwable Class

Java Throwable Class

The Throwable class in the Java.lang package, i.e Java.lang.Throwable, is the superclass of all errors and exceptions in the language. The Java Virtual Machine or the Java throw command can only toss objects that are instances of this class (or one of its subclasses). In a catch clause, only this class or one of its subclasses can be the argument type.

Class declaration

The declaration of java.lang.Throwable class is:

public class Throwable 
 extends Object 
 implements Serializable

Class Constructors

S.N Constructors & Description
1. Throwable()
Constructs a new throwable with null as its detail message.
2. Throwable(String message)
Constructs a new throwable with the specified detail message.
3. Throwable(String message, Throwable cause)
Constructs a new throwable with the specified detail message and cause.
4. protected Throwable(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace)
Constructs a new throwable with the specified detail message, cause, suppression enabled or disabled, and writable stack trace enabled or disabled.
5. Throwable(Throwable cause)
Constructs a new throwable with the specified cause and a detail message of (cause==null ? null: cause.toString()) (which typically contains the class and detail message of cause).

 

java.lang.Throwable Methods

The java.lang.Throwable class has a number of methods which are listed below:

 

Member Methods

S.N Methods & Description
1. String getMessage()
Returns the detail message string of this throwable.
2. void printStackTrace()
Prints this throwable and its backtrace to the standard error stream.
3. void printStackTrace(PrintStream s)
Prints this throwable and its backtrace to the specified print stream.
4. void printStackTrace(PrintWriter s)
Prints this throwable and its backtrace to the specified print writer.
5. String toString()
Returns a short description of this throwable.

Methods inherited

This class inherits the methods of the following class:

  • java.lang.Object

 

To open the online compiler click here.

Linode

Also Read: Java – Exceptions

 

Leave a Reply

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