
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java Throwable Class
Introduction
The Java Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement.
Class Declaration
Following is the declaration for java.lang.Throwable class −
public class Throwable extends Object implements Serializable
Class constructors
Sr.No. | Constructor & Description |
---|---|
1 |
Throwable() This constructs a new throwable with null as its detail message. |
2 |
Throwable(String message) This constructs a new throwable with the specified detail message. |
3 |
Throwable(String message, Throwable cause) This constructs a new throwable with the specified detail message and cause. |
4 |
Throwable(Throwable cause) This 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). |
Class methods
Sr.No. | Method & Description |
---|---|
1 |
Throwable fillInStackTrace()
This method fills in the execution stack trace. |
2 |
Throwable getCause()
This method returns the cause of this throwable or null if the cause is nonexistent or unknown. |
3 |
String getLocalizedMessage()
This method creates a localized description of this throwable. |
4 |
String getMessage()
This method returns the detail message string of this throwable. |
5 |
StackTraceElement[] getStackTrace()
This method provides programmatic access to the stack trace information printed by printStackTrace(). |
6 |
Throwable initCause(Throwable cause)
This method initializes the cause of this throwable to the specified value. |
7 |
void printStackTrace()
This method prints this throwable and its backtrace to the standard error stream. |
8 |
void printStackTrace(PrintStream s)
This method prints this throwable and its backtrace to the specified print stream. |
9 |
void printStackTrace(PrintWriter s)
This method prints this throwable and its backtrace to the specified print writer. |
10 |
void setStackTrace(StackTraceElement[] stackTrace)
This method sets the stack trace elements that will be returned by getStackTrace() and printed by printStackTrace() and related methods. |
11 |
String toString()
This method returns a short description of this throwable. |
Methods inherited
This class inherits methods from the following classes −
- java.lang.Object
Example: Printing Stacktrace of Throwable
The following example shows the usage of Java Throwable printStackTrace() method. We've defined a method raiseException() which throws a Throwable after setting the Stacktrace. In main method, raiseException() method is called and in catch block exception stack trace is retrieved and printed using printStackTrace() method.
package com.tutorialspoint; public class ThrowableDemo { public static void main(String[] args) { try { raiseException(); } catch(Throwable e) { // prints stacktrace for this Throwable Object e.printStackTrace(); } } public static void raiseException() throws Throwable { Throwable t = new Throwable("This is new Exception..."); StackTraceElement[] trace = new StackTraceElement[] { new StackTraceElement("ClassName","methodName","fileName",5) }; // sets the stack trace elements t.setStackTrace(trace); throw t; } }
Output
Let us compile and run the above program, this will produce the following result −
java.lang.Throwable: This is new Exception... at ClassName.methodName(fileName:5)