A brief talk on Java abnormal chain and abnormal loss _java

Source: Internet
Author: User
Tags exception handling try catch

1 . The exception chain is provided in the Java construction method. That is, we can use the construction method to continuously concatenate the anomaly into an abnormal chain ...

The reason why you need an exception is because of the understandable nature of the code, and the maintainability of reading and programs ...

We know we need a try catch for every exception we throw ... So the code is very bloated ...

If we can concatenate the exception into an exception, and then we only catch our wrapper exception, we know that runtimeexception and its derived classes can be automatically captured and handled by the JVM without a try catch.

Of course, we can define our own exception class from RuntimeException, and then through the first level of packaging, if the exception appears jwm through our custom runtimeexception direct output cause

(cause) is our anomaly chain. So all of our exceptions are exported, which reduces the number of exception handling code ...

Only throwable----> Exception runtimeexception Error provides a mechanism for constructing a method to implement an abnormal chain ... Other anomalies need to come through initcause.

Construct an abnormal connection.

The following code is a simple example of an exception connection ... You can print exceptions that occur during the entire program.

The public class Testt {public
 static void A () throws exception{  //throws an exception to the superior to handle a
 try {
  B ();
 } catch (Excepti On e) {
  throw new Exception (e);
 }
 }
 The public static void B ()  throws exception{//throws an exception to the superior to handle the
 try {
  C ();
 } catch (Exception e) {
  throw NE W Exception (e);
 }
 }
 The public static void C () throws Exception {///throws an exception to the superior to handle
  the null pointer exception in the "C exception chain ..." in the try {throw new nullpointerexception); 
   } catch (NullPointerException e) {
  throw new Exception (e);
 }
 } 
 public static void Main (String[]args) { 
 try {
  a ();
 } catch (Exception e) {
  e.printstacktrace ();
 }
 
 }
}

2, try catch ... Finally, there is a flaw is the exception. For example, three try catch nesting together ... An internal 2 Try catch can omit the catch .... Direct Try finally ...

Look at the code below, we've lost 2 exception messages.

public class MyTest {public
 void open () throws exception{
 throw new Exception () {public
  String toString () {
   return This.getclass (). GetName () + "ceryimmportexception";
  };
 } ; 
 }
 public void Close () throws exception{

 throw new Exception () {public
  String toString () {
  
  return This.getclass (). GetName () + "Close Exception";}
  ;
 } ;
 }
 public void Three () throws exception{
    throw new Exception () {public
     String toString () {
      
      return This.getclass (). GetName () + "three";}
     ;
    } ; 
 }
 public static void Main (STRING[]AGRS) {  
 MyTest mt=new MyTest ();
 try{
 try{
 try{
  mt.open ();
   } Finally
 {
   System.out.println ("delete Open"); 
   Mt.close ();
 }
 finally{
  System.out.println ("delete close");
  Mt.three ();
  
 }
 catch (Exception ex) {
  ex.printstacktrace ();}}} 


This article on the Java abnormal chain and abnormal loss is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.