Android tricks: the ideal way to write try/catch clses

Source: Internet
Author: User
Tags finally block

Exceptions and exception handling are an important part in most applications. and Modern Programming Languages Support exceptions very well. Most of them provide try/catch/finally clses for developers to handle exceptions.
Here we talk about how to write try/catch/finally clses in a more elegant way.

Here, we take I/O operations as our implemented strated example, Because I/O conerns three important steps when using: initialization, exception handing and cleanup.

Generally, some programmer might write like this:

// Declaration of objectsInputStream input = null;OutputStream output = null;try {     // Initialization input and output     input = new FileInputStream("readme.txt");     output = new FileOutputStream("ic_card.txt");          // Operations     int seg = 0;     byte[] buf = new byte[4096];     while ((seg = input.read(buf)) != -1) {          output.write(buf, 0, seg);     }} catch (FileNotFoundException e) {     // handling exception} catch (IOException e) {     // handling exception} finally {     // cleanup     try {          if (input != null) {             input.close();          }          if (output != null) {             output.close();          }      } catch (IOException e) {           // handling exception      }}

Such style has the following problems:

  1. There are extra try/catch inside outer try/catch to catch the same exception, which is might be redundant
  2. The variable input/output are declared beyond where they are needed, which have unnecessary life circle
  3. Two try/catch and try/catch in finally, which might be not elegant enough

The suggested way is like this:

try {     // Declarations     InputStream input = null;     OutputStream output = null;     try {           // Initialization input and output           input = new FileInputStream("readme.txt");           output = new FileOutputStream("ic_card.txt");           // Operations           int seg = 0;           byte[] buf = new byte[4096];           while ((seg = input.read(buf)) != -1) {               output.write(buf, 0, seg);           }      } finally {           // cleanup codes           if (input != null) {               input.close();           }           if (output != null) {               output.close();           }      }} catch (FileNotFoundException e) {     // handling exception} catch (IOException e) {     // handling exception}

This try/finally try/catch style is superior to previous version is because, it finishes all the tasks: Declaration, initialization, operations and cleanup. all blocks are inside big try/catch so that all exceptions are get caught;
Cleanup is inside try/finally so that we ensure to release taken resources. It also looks elegant.

Such try/finally try/catch clses, however, have a fatal defect that exceptions cocould be ignored and lost. in try/finally, finally block gets executed even if when exceptions are thrown in try block. if finally block also throws
Conditions, ones thrown in try blocks are ignored and lost. Outer catch clses will never catch them. In all, this style might not catch all conditions thrown in try blocks.

You can apply this try/finally try/catch style when:

  1. Cleanup codes are not much and quite simple, like closing Io streams, shutdown connection. etc.
  2. Cleanup codes are not likely to throw except tions, or much less than initializations and operations.
  3. You do not care what exceptions are, but whether there are exceptions thrown

Besides these, you should use try/catch/Finally, when specific exceptions are important in particle.

// Delcarationstry {     // Initializations          // Operations} catch {     // handling exceptions} finally {     // cleanup}

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.