The first method, close everything

Source: Internet
Author: User

As a start-and-end person, after starting a task, no matter what the outcome of the task is, you must finish the work.ProgramThis is also the case. After applying for the resource access permission, you must release the resource after it is used up.

In most cases, the release operation is irrelevant to the running result and status of the program. Generally, it will be written in finally blocks. But the problem arises again, because in most cases, an exception is thrown when you close this function. This exception often does not affect the operation of our program.

  Try  {
// Do something
} Catch (Exception ex ){
// Do exception
} Finally {
Try {
In. Close ();
} Catch (Exception ex ){
}
Try {
Out. Close ();
} Catch (Exception ex ){
}
}

This writing method will affect the readability and appearance of our program. Of course, it will increase the workload of our programmers. Therefore, it is necessary to close 10 thousand bits.

 

 

Let's analyze which objcet: inputstream, outputstream, writer, and reader calls the close function in general. (These things have implemented the closeable interface after jdk1.5 ). Connection, statement, and resultset. To implement such a function, you only need to consider these situations.

 

We have two ways to implement this function. The first is to define n close functions with different parameters and implement type differentiation using compiler capabilities. There is another way to implement only one close (Object O) function. I recommend this. Why? See Implementation

Code

    Public     Static     Void Close (Object O ){
If (O ! = Null ){
Try {
If (O Instanceof Connection ){
(Connection) O). Close ();
} Else If (O Instanceof Statement ){
(Statement) O). Close ();
} Else If (O Instanceof Resultset ){
(Resultset) O). Close ();
} Else If (O Instanceof Outputstream ){
(Outputstream) O). Close ();
} Else If (O Instanceof Inputstream ){
(Inputstream) O). Close ();
} Else If (O Instanceof Writer ){
(Writer) O). Close ();
} Else If (O Instanceof Reader ){
(Reader) O). Close ();
} Else {
Class C = O. getclass ();
C. getmethod ( " Close " , New Class [ 0 ]). Invoke (O, New Object [ 0 ]);
}
} Catch (Exception ex ){

}
}
}

 

The only clever thing is that we have added the 10 thousand close function to this function. As long as the object has an accessible, non-parameter close method, we will call and execute it.

 

Now we can makeCodePreliminary beautification

  Try{
//Do something
}Catch(Exception ex ){
//Do exception
}Finally{
Close (in );
Close (out );
}

 

 

As a person who pursues perfection (in fact, it is too lazy), it will not satisfy this degree of simplification. Continue

    Public     Static     Void  Close (object... objs ){
For ( Int I = 0 , Size = Objs = Null ? 0 : Objs. length; I < Size; I ++ ){
Close (objs [I]);
}
}

The whole world is quiet

 

 

  Try{
//Do something
}Catch(Exception ex ){
//Do exception
}Finally{
Close (In, out );
}

 

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.