New Features of Java SE7 capture multiple types of exceptions and re-throw exceptions Using Improved Type checks

Source: Internet
Author: User

This article covers the following topics:

    Handle multiple types of exceptions and re-throw exceptions that use broader types of checks to handle multiple types of exceptions

    In Java SE 7 and later versions, a simplecatchBlocks can handle multiple types of exceptions. This feature can reduce repeated code and the temptation to catch overly broad exceptions.

    Consider the following example:catchThe block contains duplicate code:

    catch (IOException ex) {     logger.log(ex);     throw ex;catch (SQLException ex) {     logger.log(ex);     throw ex;}

    In the release versions earlier than Java SE 7exThere are different types, and it is difficult to create a public method to eliminate repeated code.

    The following example is valid in Java SE 7 and later versions. It eliminates repeated code:

    catch (IOException|SQLException ex) {    logger.log(ex);    throw ex;}

    catchClause specifies the exception types that can be processed by the statement block (|.

    Note:: IfcatchBlock Processing has multiple exception types.catchThe parameter is implicitly declaredfinal。In this example,catchParametersexYesfinalThereforecatchYou cannot specify any value for the block.

    CompilecatchBlock-generated bytecode is more than compiledcatchBlock, but each can only process one exception type produces less bytecode (so it is better ). In the bytecode generated by the compiler,catchThe block is not repeated; the bytecode does not replicate the exception processor.

    Throw an exception that uses a broader type check.

    Compared with the previous Java SE release version, the Java SE 7 compiler can perform more precise analysis on the re-throw exceptions. This allows you to declarethrowsClause to specify a more specific exception type.

    Consider the following example:

      static class FirstException extends Exception { }  static class SecondException extends Exception { }  public void rethrowException(String exceptionName) throws Exception {    try {      if (exceptionName.equals("First")) {        throw new FirstException();      } else {        throw new SecondException();      }    } catch (Exception e) {      throw e;    }  }

    In this exampletryBlocks can be thrownFirstExceptionYou can also throwSecondException . Assume that you wantthrowsThe exception type is specified in the clause. You cannot do this in versions earlier than Java SE 7. BecausecatchClause exception parameter e isExceptionType, and the catch Block re-throws this exception parameter e, so you can only in the rethrowException method declarationthrowsSpecify the exception typeException.

    However, in Java SE 7, you canthrowsSpecify the exception typeFirstExceptionAndSecondException. The Java SE 7 compiler can detectthrow eThe exception thrown by the statement must come fromtryBlock, andtryThe exception thrown by the block can only beFirstExceptionAndSecondException. Even ifcatchThe exception parameter e of the clause is of the typeExceptionThe compiler can also detect that it isFirstExceptionOrSecondExceptionInstance:

      public void rethrowException(String exceptionName)  throws FirstException, SecondException {    try {      // ...    }    catch (Exception e) {      throw e;    }  }

    IfcatchBlockcatchThis analysis fails if the parameter is specified to another value. However, ifcatchThe parameter is specified to another value. You mustthrowsSpecify the exception typeException.

    Specifically, in Java SE 7 and later versionscatchClause declares one or more exception types and throwscatchThe compiler verifies whether the type of the thrown exception meets the following conditions:

      tryYou can throw a block. Previous catchBlock cannot handle it. It is catchA subclass or superclass of an exception parameter in the clause.

      Java SE 7 compiler allows yourethrowExceptionIn the method declarationthrowsClause specifies the exception typeFirstExceptionAndSecondException. Because you can throwthrowsAny type of superclass declared in.

      In versions earlier than Java SE 7, the exception you throw cannot becatchClauseSuperclass. In versions earlier than Java SE 7, the compilerthrow e"Unreported exception" is generated here.Exception; Must be caught or declared to be thrown "error. The compiler checks whether the thrown exception type can be specifiedrethrowExceptionMethod DeclarationthrowsAny type declared in the clause. However, the catch parameter e type isException, It isFirstExceptionAndSecondException'sA superclass, not a subclass.


      This article is translated from the Oracle official documentation workshop!

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.