"Throw" the offending again.

Source: Internet
Author: User
Tags stack trace throwable

In some cases, we would like to roll back the offending that has just occurred, especially when all possible breaches are caught using exception. Since we already have a handle to the current violation, simply throw the handle back. The following is an example:
catch (Exception e) {
System.out.println ("One violation has been created");
throw e;
} The
throws a new offence into an unauthorized controller that leads to an unauthorized entry into a higher level environment. Any further catch clauses used for the same try block will still be ignored. In addition, everything related to the offending object is preserved, so a higher controller that captures a specific type of violation can extract all the information from that object.
If we simply roll back the current violation, the information we print out about the offence in Printstacktrace () will correspond to the origin of the offence and not to the place where it was thrown back. If you want to install a new stack trace, call Fillinstacktrace (), which returns a particular offending object. The creation of this violation is as follows: Populate the current stack of information into the original offending object. Its form is listed below:
 

: Rethrowing.java
//Demonstrating fillinstacktrace () public

class Rethrowing {public
  static void F () Throws Exception {
    System.out.println (
      "originating the Exception in F ()");
    throw new Exception ("thrown from F ()");
  }
  public static void G () throws Throwable {
    try {
      f ();
    } catch (Exception e) {
      System.out.println (
        "Inside g (), E.printstacktrace ()");
      E.printstacktrace ();
      Throw e; //
      throw E.fillinstacktrace ()///{}
    }
  public
  static void
  main (string[] args) throws Throwable {
    try {
      g ();
    } catch (Exception e) {
      System.out.println (
        "Caught in Main," E.printstacktrace () ");
      E.printstacktrace ();}}
///:~


The most important line number is marked in the annotation. Note that line 17th is not set to comment lines. Its output results are as follows:

Originating the exception in F ()
Inside g (), E.printstacktrace ()
Java.lang.Exception:thrown from F ()
        at RETHROWING.F (Rethrowing.java:8) at
        rethrowing.g (rethrowing.java:12) at
        Rethrowing.main (Rethrowing.java :
caught in Main, E.printstacktrace () Java.lang.Exception:thrown from
F () at
        RETHROWING.F ( Rethrowing.java:8) at
        rethrowing.g (rethrowing.java:12) at
        Rethrowing.main (rethrowing.java:24)


Therefore, the offending stack path will, in any case, remember its true starting point, no matter how many times it has been repeatedly "tossed".
If you mark the 17th line (into the comment line) and undo the callout for line 18th, the Fillinstacktrace () is replaced with the following result:

Originating the exception in F ()
Inside g (), E.printstacktrace ()
Java.lang.Exception:thrown from F ()
        at RETHROWING.F (Rethrowing.java:8) at
        rethrowing.g (rethrowing.java:12) at
        Rethrowing.main (Rethrowing.java :
caught in Main, E.printstacktrace () Java.lang.Exception:thrown from
F () at
        RETHROWING.G ( rethrowing.java:18) at
        Rethrowing.main (rethrowing.java:24)


Because of the use of Fillinstacktrace (), line 18th becomes a new starting point for the violation.
for G () and main (), the Throwable class must appear in the offending specification because Fillinstacktrace () generates a handle to the Throwable object. Since Throwable is a base class for exception, it is possible to get an object that can be "tossed" (with a throwable attribute) but is not a exception (violation). Therefore, the handle used for exception in main () may lose its target. To ensure that everything is in order, the compiler forces throwable to use an offending specification. For example, a violation of the following procedure will not be caught in main ():

: Throwout.java public
class Throwout {public
  static void
  main (string[] args) throws Throwable {
    try {
      throw new Throwable (); 
    } catch (Exception e) {
      System.out.println ("Caught in Main ()");}}}
///:~


It is also possible to "toss" a different offence from a captured violation. But if you do, you get the same effect as using Fillinstacktrace: The information about the origin of the violation is lost, and we leave information about the new throw. As shown below:

: Rethrownew.java
//Rethrow a different object from the "one"
//Is caught public

class Rethrownew {
   public static void F () throws Exception {
    System.out.println (
      "originating the Exception in F ()");
    throw new Exception ("thrown from F ()");
  }
  public static void Main (string[] args) {
    try {
      f ();
    } catch (Exception e) {
      System.out.println (
        "Caught in Main, E.printstacktrace ()");
      E.printstacktrace ();
      throw new NullPointerException ("from Main");}}
///:~


The output is as follows:

Originating the exception in F ()
caught in Main, E.printstacktrace ()
Java.lang.Exception:thrown from F () At
        rethrownew.f (rethrownew.java:8) at
        rethrownew.main (rethrownew.java:13)
Java.lang.NullPointerException:from Main at
        Rethrownew.main (rethrownew.java:18)


The last violation only knows that you are from main (), not from F (). Note that Throwable is not required in any of the offending specifications.
Never have to care about how to clear the previous violation, or any other violation. They all belong to a memory heap based object created with new, so the garbage collector automatically clears it.

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.