Spread the love

Class Throwable in Java

Spread the love

Class Throwable in Java

The throwable class is the superclass of all errors and exceptions in the java that can be thrown by the throw keyword. All exceptions and errors are subclasses of the Throwable. Only objects which are an instance of this class or one of its subclass are thrown by the java virtual machine. It can also be thrown by java’s statement.

So only this class or it’s subclass can be the argument type in catch clause. A throwable object contains a snapshot of the execution stack of its thread at the time it was created. It can also contain a message string that gives more information about the error or exception.

A Throwable object can have an associated message that provides more detail about the particular error or exception that is being thrown.

The Throwable class provides a method that outputs information about the state of the system when an exception object is created. This method can be useful in debugging Java programs.

The getMessage() method retrieves any error message associated with the exception or error.

printStackTrace() prints a stack trace that shows where the exception occurred.

fillInStackTrace() extends the stack trace when the exception is partially handled, and then re-thrown.

The structure of the throwable class given below:

public class java.lang.Throwable extends java.lang.Object implements java.io.Serializable{
//constructors:
public Throwable();
//creates a new throwable object with no detail message. The stack trace is automatically filled in.
public Throwable(String message);
//creates a new throwable object with the detail message provided in the argument. The stack trace is automatically filled in.
//Methods:
public native Throwable fillInStackTrace();//fills the stack trace of this Throwable Object with the current stack trace,adding to any previous information in the stack trace.
public String getMessage(); //returns the details message about the exception that has occurred. This message is initialized in the Throwable constructor.
public void printStackTrace();//prints the result of toString() along with the stack trace.
public void printStackTrace(PrintStream ps);//prints the result of toString() along with the stack trace to the Stream like System.err,the Error output stream
public String toString();//returns the name of the class concatenated with the result of getMessage().Short description of Throwable Object
public Throwable getCause();//returns the cause of the exception as represented by a Throwable Object
public stackTraceEmenet[] getStackTrace()//returns an error containing each element on the stack trace.The element at index-0 represents the top of call stack and the last element in the Array represents at the bottom of the call stack.
public void printStackTrace(PrintWriter ps)//prints the stack trace to the indicated print writer.
public String getLocalizedMessage();
}
 

The details of the Throwable class is given as:

public Throwable();

public Throwable() constructor creates a Throwable object with no associated message. This constructor calls fillInStackTrace() so that information is available for printStackTrace().

public Throwable(String message);

public Throwable(String message) constructor Create a Throwable object with an associated message. This constructor calls fillInStackTrace() so that information is available for printStackTrace().

READ  Static Keyword Simplified in Java
READ  SequentialInputStream in Java

Parameter
message – A message string to be associated with the object.

public native Throwable fillInStackTrace();

public native Throwable fillInStackTrace() method puts stack trace information in this Throwable object. It is not usually necessary to explicitly call this method since it is called by the constructors of the class. However, this method can be useful when rethrowing an object. If the stack trace information in the object needs to reflect the location that the object is rethrown from, fillInStackTrace() should be called.

This method returns a reference to this object.

public String getLocalizedMessage();

public String getLocalizedMessage() method creates a localized version of the message that was associated with this object by its constructor.

The getLocalizedMessage() method in Throwable always returns the same result as
getMessage().

A subclass must override this method to produce a locale-specific message.

This method returns a localized version of the String object associated with this Throwable object, or null if there is no message associated with this object.

public String getMessage();

public String getMessage() method returns any string message that was associated with this object by its constructor.

This method returns the String object associated with this Throwable object, or null if there is no message associated with this object.

public void printStackTrace();

public void printStackTrace() method outputs the string representation of this Throwable object and a stack trace to System.err.

public void printStackTrace(PrintStream ps);

public void printStackTrace(PrintStream ps) method outputs the string representation of this Throwable object and a stack trace to the specified PrintStream object.
Parameter
ps – A java.io.PrintStream object.

public void printStackTrace(PrintWriter ps);

public void printStackTrace(PrintWriter ps) method outputs the string representation of this Throwable object and a stack trace to the specified PrintWriter object.
Parameter
ps – A java.io.PrintWriter object.

public String toString();

public String toString() method returns a string representation of this Throwable object.
This method returns a string representation of this object.
Apart from these Throwable class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)
READ  Class PropertyResourceBundle in Java
READ  Class ObjectOutputStream in Java




Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special keyword called...

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent Posts

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special...

Learn What Primary Tools For Test Automation

Primary Tools For Test Automation According to the Gartner report...

EDITOR PICKS


Spread the love