The top-level exception class in Java Throwable

Source: Internet
Author: User
Tags serialization throwable

The Throwable class is the parent class of the entire exception system class, and of course the parent that ends up in the end is, of course, attributed to the object class. The Throwable class implements the Serializable interface, which means that throwable can be serialized, inheriting from the object class, whose subclasses are mainly the error and exception classes and a Stackrecorder class (not very common).

So here are a few questions to think about:

1. Why serialization.

2. The source code does not see the inheritance object this behavior, but the default is inherited how to do.

3. What do these two subclasses do? Why not directly in Throwable a class to handle.


Serialization Issues

serialization (serialization) converts the state information of an object into a form that can be stored or transmitted. During serialization, an object writes its current state to a temporary or persistent storage area. Later, the object can be recreated by reading or deserializing the state of the object from the store.

Three kinds of cases:

1. Store the object on the hard drive.

2. Transfer objects over the network.

3. When the object is transmitted by means of RMI remote invocation.

in these three cases, serialization is required and transmitted.

The output stream is used in the Throwable class for output and the object as an output stream object, which requires that the serialization interface be implemented so that it can be serialized to output as an object in the output stream.

[HTML]  View Plain  copy   Private synchronized void writeobject (objectoutputstream s)            throws IOException {            // ensure that the stacktrace field is  initialized to a           // non-null  value, if appropriate.  as of jdk 7, a null stack            // trace field is a valid  value indicating the stack trace            // should not be set.            getourstacktrace ();               Stacktraceelement[] oldstacktrace = stacktrace;           try {                if  (stacktrace = = null)                     stackTrace = SentinelHolder.STACK_TRACE_SENTINEL;                s.defaultwriteobject ();            } finally {                stackTrace = oldStackTrace;            }       }  

Inherit object class

1. At compile time, if the class does not inherit the parent class, it will automatically add the compiled information of the related class that inherits the parent object, so that when the subsequent virtual device interprets the execution, it can be handled by the existing parent class.

2. When the virtual machine executes, if there is still a class that does not have a parent class, the parent class is still the default object.

The first case belongs to the compiler for processing, and the second is to do the proper processing on the virtual machine.


sub-class error and exception

The error is mainly used to represent the exception information inside Java and virtual machines, while the exception exception is due to various problems in the program, which require the user to notice and catch the exception.

from the design of anomaly class, the designer's abstract thinking and design level is impressive, through a class to abstract all the common methods and representations of all the anomalies and the entity structure of their expression, and through the way of inheritance, we make a horizontal division of the domain of anomaly. It is divided into error and exception two parallel exception types, and then the two will again act as the parent of the respective type of exception, because each exception also has a different classification, creating a series of classes to inherit the two exceptions above to derive the new exception type partition. This continuous succession, gradually refined to each specific type of anomaly, forming a rich class of exceptions.

from the extensibility point of view, because Throwable implementation is the general part of the exception class, then, if there is a special anomaly classification, you can inherit throwable way to extend the exception system, of course, our most commonly used may not involve direct inheritance throwable.


Source code Interpretation


The default is an empty stacktrace node array initialized to the empty Stack,getourstacktrace () method is mainly to get the information of the current node exception, get the exception information on the stack, traverse each exception information, Assign values to StackTrace for saving.

[HTML] view plain copy private static final stacktraceelement[] Unassigned_stack = new Stacktraceelement[0]; Private stacktraceelement[] StackTrace = unassigned_stack;

[HTML]  View Plain  copy   Private synchronized stacktraceelement[] getourstacktrace ()  {           // initialize stack trace  field with information from           //  backtrace if this is the first call to this method            if  (stacktrace == unassigned_stack | |                 (stacktrace ==  Null && backtrace != null)  /* Out of protocol state  */)  {               int depth  = getstacktracedepth ();                stacktrace = new stacktraceelement[depth];                for  (int i=0; i < depth

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.