Resolves the parent class of all errors and exceptions in Java Java.lang.Throwable_java

Source: Internet
Author: User
Tags exception handling getmessage null null terminates throwable

In the Java language, the base class for the error class is Java.lang.Error, and the base class for the exception class is java.lang.Exception.
1) Same point: Java.lang.Error and Java.lang.Exception are java.lang.Throwable subclasses, so java.lang.Error and java.lang.Exception themselves and their subclasses can be used as throw Like: throw new Myerror (); and throw new MyException (); The Myerror class is a subclass of Java.lang.Error, and the MyException class is a subclass of Java.lang.Exception.
2 different points: Java.lang.Error itself and its subclasses do not need the support of the Try-catch statement, you can return the method at any time, such as the following method definition:

Public String MyMethod () { 
throw new Myerror (); 
} 

Where the Myerror class is a subclass of the Java.lang.Error class.
The java.lang.Exception itself and its subclasses require support for the Try-catch statement, as the following method definition is incorrect:

Public String MyMethod () { 
throw new myexception (); 
} 

The correct method is defined as follows:

Public String MyMethod () throws MyException { 
throw new myexception (); 
} 

Where the MyException class is a java.lang.Exception subclass.

A Java exception is an object created when a Java program encounters an abnormal condition, which encapsulates the exception information, and the root class of the Java exception is java.lang.Throwable. The entire class has two direct subclasses Java.lang.Error and java.lang.Exception.Error are serious errors that the program itself cannot recover. Exception represents an exception error that can be caught and handled by the program. The JVM uses the method call stack to track a series of method call procedures in each thread , which holds the local information for each calling method. For a stand-alone Java program, you can always go to the main method of the program. When a new method is invoked, the JVM puts the stack structure that describes the method on top of the stack, and the method at the top of the stack is the correct execution method. When a Java method executes normally, The JVM returns the stack structure of the method when it is shot from the call stack, then continue with the previous method. If the Java method throws an exception during the execution of the code, the JVM must find a catch block code that catches the exception. It first looks at whether the current method has such a catch code block, and if it does, executes the Catch code block, or the JVM's stack structure of the method is popped back to the call stack, continue to find the appropriate catch code block in the previous method. Finally, if the JVM catches up to the main () method, which is always throwing the exception to the main () method, there is still no code block found for the exception handling. The thread terminates abnormally, and the application terminates if the thread is the main path, at which point the JVM throws the exception directly to the user and sees the original exception information on the user's terminal.

Java.lang.throwable Source Code resolution

Package Java.lang; 
Import java.io.*; /** * * Throwable is the parent class for all error and Exceptiong * Note that it has four constructors: * Throwable () * Throwable (String message) * Throwable (THROWABL  E cause) * Throwable (String message, throwable cause) */public class Throwable implements Serializable {private 
 
  Static final Long serialversionuid = -3042686055658047285l; 
  /** * Native code saves some indication of the stack backtrace in this slot. 
 
  * Private transient Object backtrace; 
 
  /** * Describes the information for this exception/private String detailmessage; /** * indicates that the current exception is caused by that throwable * If NULL indicates that this exception is not caused by another throwable * If this object is the same as itself, the cause object for this exception has not been initialized/private Throwa 
 
  ble cause = this; 
 
  /** * Describes an array of anomaly trajectories * * private stacktraceelement[] stacktrace; /** * constructor, cause the object is not initialized can be initialized later using Initcause * Fillinstacktrace can be used to initialize its exception trajectory array/public throwable () {Fill 
  Instacktrace (); }/** * Constructor/public throwable (String message) {//Fill exception trajectory array FillinstacktracE (); 
  Initialize the exception description information detailmessage = message; 
   /** * constructor, cause represents the cause object/public throwable (String message, throwable cause) {fillinstacktrace (); 
   detailmessage = message; 
  This.cause = cause; 
   }/** * Constructor/public throwable (Throwable cause) {fillinstacktrace (); 
   Detailmessage = (cause==null null:cause.toString ()); 
  This.cause = cause; 
  /** * Get more information/public String GetMessage () {return detailmessage; 
  /** * Get more information/public String Getlocalizedmessage () {return getMessage (); 
  /** * Get cause object/public Throwable Getcause () {return (Cause==this null:cause); /** * Initializes the cause object, which can be invoked only once without initialization/public synchronized Throwable initcause (Throwable cause) {//If not 
   
   is an uninitialized state throws an exception if (This.cause!= this) throw new IllegalStateException ("Can ' t overwrite cause"); The cause object to set is equal to itself throws an exception if (cause = = this) throw new IllegalaRgumentexception ("Self-causation not permitted"); 
   Setting cause object this.cause = cause; 
  Returns the object returning this set of causes;   
   /** * String representation/public string toString () {string s = GetClass (). GetName ();  
   String message = Getlocalizedmessage (); return (message!= null)? 
  (S + ":" + message): s; 
  /** * Print out error trajectory/public void printstacktrace () {printstacktrace (system.err); /** * Print out error trajectory/public void printstacktrace (PrintStream s) {synchronized (s) {//Call the current object's ToString 
   Method S.println (this); 
    
   Gets the exception trajectory array stacktraceelement[] trace = Getourstacktrace (); 
 
   Prints the string representation for each element for (int i=0 i < trace.length; i++) s.println ("\tat" + trace[i)); 
    
   Get Cause object Throwable ourcause = Getcause (); 
   Recursively prints out the cause object's information if (ourcause!= null) ourcause.printstacktraceascause (S, trace); }/** * Print the information of the cause object * @param S-Print stream * @param causedtrace exception Trajectory for exceptions caused by this object 
  * * private void Printstacktraceascause (PrintStream s, stacktraceelement[] causedtrace) {//Get current 
   Exception trajectory stacktraceelement[] trace = Getourstacktrace (); 
   M is the last element position of the current exception trajectory array, the last element int m = trace.length-1, n = causedtrace.length-1 of the exception trajectory array that caused the current object//n;  
    Loops from the back of two arrays, if they are equal, until they are unequal or array at the end while (M >= 0 && N >=0 && trace[m].equals (causedtrace[n)) { m--; 
   n--; 
   
   //the same number int framesincommon = trace.length-1-M; 
   Print out different error trajectories s.println ("caused by:" + this); 
   for (int i=0 i <= m; i++) s.println ("\tat" + trace[i)); If there is the same, print out the same number if (Framesincommon!= 0) s.println ("t ... 
 
   "+ Framesincommon +"); 
   Obtains the cause object of this object, and recursively prints out the information throwable ourcause = Getcause (); 
  if (ourcause!= null) ourcause.printstacktraceascause (S, trace);  /** * Print out error trajectory/public void printstacktrace (PrintWriter s) {synchronized (s) {s.println (this);
    Stacktraceelement[] trace = Getourstacktrace (); 
 
    for (int i=0 i < trace.length i++) s.println ("\tat" + trace[i)); 
    Throwable ourcause = Getcause (); 
   if (ourcause!= null) ourcause.printstacktraceascause (S, trace);  }/** * Printing the information of the cause object/private void Printstacktraceascause (PrintWriter s, stacktraceelement[) 
 
   Causedtrace) {//Assert thread.holdslock (s); 
   Compute number of frames in common between this and caused stacktraceelement[] trace = Getourstacktrace (); 
   int m = trace.length-1, n = causedtrace.length-1; 
   while (M >= 0 && N >=0 && trace[m].equals (causedtrace[n)) {m--; n--; 
 
   int Framesincommon = trace.length-1-M; 
   S.println ("caused by:" + this); 
   for (int i=0 i <= m; i++) s.println ("\tat" + trace[i)); if (Framesincommon!= 0) s.println ("t ... 
 
   "+ Framesincommon +"); Recurse If we have a cause throwable Ourcause = Getcause (); 
  if (ourcause!= null) ourcause.printstacktraceascause (S, trace); 
 
  /** * Fill anomaly trajectory * * public synchronized Native Throwable Fillinstacktrace (); /** * Returns the copy of the current exception path/public stacktraceelement[] Getstacktrace () {return (stacktraceelement[)) getourstacktr 
  Ace (). Clone (); /** * Get the current exception trajectory/private synchronized stacktraceelement[] Getourstacktrace () {//If the first call to this method initializes the exception 
   Trajectory array if (StackTrace = null) {//Get exception trajectory depth int depth = getstacktracedepth (); 
    
   Creates a new array of exception trajectories and fills it stacktrace = stacktraceelement[depth]; for (int i=0 i < depth; i++) Stacktrace[i] = getstacktraceelement (i);//Get the exception trajectory of the position mark} return Stacktrac 
  E /** * Set exception trajectory/public void setstacktrace (stacktraceelement[] stacktrace) {//Copy settings parameter Stacktraceele 
   
   Ment[] Defensivecopy = (stacktraceelement[]) stacktrace.clone (); Throws an exception for if the set parameter has an empty element for (int i = 0; i < deFensivecopy.length; 
 
   i++) if (defensivecopy[i] = = null) throw new NullPointerException ("stacktrace[" + i + "]"); 
  Sets the exception trajectory for the current object this.stacktrace = Defensivecopy; 
 
  /** * The depth of the anomaly trajectory, 0 means that the/private native int getstacktracedepth () cannot be obtained. 
 
  
  /** * Get the abnormal trajectory of the positioning mark * * Private native stacktraceelement getstacktraceelement (int index); 
   Private synchronized void WriteObject (Java.io.ObjectOutputStream s) throws IOException {getourstacktrace (); 
  S.defaultwriteobject (); 
 } 
}

Related Article

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.