GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java Throwable Class
Selected Reading
Java Throwable ClassIntroductionThe 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 DeclarationFollowing is the declaration for java.lang.Throwable class − public class Throwable
extends Object
implements Serializable
Class constructors
Class methods
Methods inheritedThis class inherits methods from the following classes −
Example: Printing Stacktrace of ThrowableThe 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;
}
}
OutputLet 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) Advertisements
| ||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |