Java exception Handling-don't ignore or obscure exceptions

Source: Internet
Author: User
Tags add define object end exception handling finally block
Exception handling exception handling is a powerful and practical feature of Java that makes exceptions easier to use, but also makes it more difficult to use appropriately-the practical Java Java exception is composed of try/catch/finally segments, They can also be nested arbitrarily (nest). If there are multiple exceptions in the program, but only the last occurrence of the exception returned to the original caller, a number of the preceding exceptions will be obscured or lost-if we do not deal effectively! Obviously, we should not ignore exceptions, or catch and not deal with, which will lead to the subsequent procedures more error-prone and not very good to judge the source of the error, and should not lose some exceptions in the throw process, because usually need to be based on exception information to generate a response, and we have to get each should get the exception information. The following is a simple example of this bad situation will often occur, at least I have written a lot of this generation: Ppackage test; public class Hideexceptionyn {public static void main (string[] args) {Hideexceptionyn Hideexceptionyn = new Hi        Deexceptionyn ();        try {hideexceptionyn.foo ();        The catch (Exception ex) {SYSTEM.OUT.PRINTLN ("in Main, caught the Exception:" + ex.getmessage ()); }
   }     public void foo () throws Exception {         Try {            throw new Exception ("A-E");        } catch (Exception ex) {             throw new Exception ("Second E");       } finally {             throw new Exception ("Third Exception");        }   } The results of the program run, only print out: in Main, caught the Exception:third exception visible, the single and Second e are lost , this is the result I don't want to see. The specifics can be described as: if you read a file in a try segment, you might throw a filenotfoundexception or read a failed ioexception, and we usually have to close the open file in the finally section. To release the Non-memory resource, which may throw a ioexception, and the last exception will obscure all the exceptions thrown above, and if not, the initial caller will have to end this exception!

Based on experience, we can create a vector object that holds all the exceptions thrown during the running of the program, and finally returns the vector object to the caller to solve the problem. Of course, we should handle the anomaly as much as possible in place! The discussion here is about the need to continuously throw the inner layer to the outer layers. The simple approach is to define a vector (ArrayList) object when entering a method that might occur, and then add the exception object to the vector in each catch block. Then, at the end of the finally block, the size of the vector is zero, and not zero indicates that an exception has occurred--throw it to the caller. In this way, the caller receives all the exceptions from its start to the running process that returned it. The sample code is as follows: Vector vector=new vector (); Try{//....}catch (xxxexception e) {    Vector.add (e);    }catch (yyyexception E2) {    Vector.add (E2); finally{  try{ //...} catch (dddexcetpion E3) {  Vector.add (E3);}   if (vector.size ()!=0) {throw new userdefineexception (vector);       }}// You need to define a Userdefineexception class: There is a vector class field, and there are Setvector and Getvector () methods. The simple approach is to define a vector (ArrayList) object when entering a method that might occur, and then add the exception object to the vector in each catch block. Then, at the end of the finally block, the size of the vector is zero, and not zero indicates that an exception has occurred--throw it to the caller. In this way, the caller receives all the exceptions from its start to the running process that returned it. The sample code is as follows: Vector vector=new vector (); Try{//....}catch (xxxexception e) {    Vector.add (e);    }catch (yyyexception E2) {    Vector.add (E2); finally{  try{ //...} catch (dddexcetpion E3) {  Vector.add (E3);}   if (vector.size ()!=0) {throw new userdefineexception (vector);       }}// You need to define a Userdefineexception class: There is a vector class field, and there are Setvector and Getvector () methods.



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.