Groovy Exploration Closure II

Source: Internet
Author: User
Tags closure

Since programming into the object-oriented era, we have been working tirelessly with the process-oriented struggle. Think of all the patterns that we all love to use and serve as classic bang of four, and how much is for if ... else ... Such statements are optimized. But even if we do a lot of the above, some process-oriented language still stands there laughing at our incompetence.

A Solve the classic Try...catch problem

Yes, Try...catch is a classic problem we often encounter. Take a look at the following code:

publicstatic List getdatafromexcel (String filename,int beginrow,int endrow,modelintf model,string[]     Inputcols,list messages)
{
List retn = new ArrayList ();
InputStream fs = null;
Workbook WB =null;      
Try
{
fs = new FileInputStream (fileName);
WB = Workbook.getworkbook (FS);
Sheet sh = wb.getsheet (0);
if (logger.isdebugenabled ()) {
Logger.debug ("sheetname =" + Sh.getname ());
}
int len = inputcols.length;
String[] values = new String[len];
.....
}
catch (Exception e)
{
if (logger.isdebugenabled ()) {
Logger.debug ("Excelre Ader ", e);    
}
E.printstacktrace ();
       
finally
{
try {
if (fs!=null)
{
Fs.close ();
       
if (wb!=null)
{
Wb.close ()

} catch (IoexcePtion e) {
//TODO auto-generated catch block
E.printstacktrace ();

}
Return RETN
}

The code above is a piece of code that I read from an Excel document using the JXL API, and in order to focus on what we're talking about, I'm going to delete a section of the code. In this way, we can see clearly the try...catch statements in the code, and the role they play.

In my code for reading an Excel document, there are five or six similar methods. They all use such try...catch statements, and they do the same thing in the Catch statement segment and finally statement section, except that they do something different in the Try statement segment.

Do we all want to copy the same catch statement and finally sentence segments into each method? If you need to maintain a catch statement segment and a finally statement segment, do you want to modify each method?

See here, you must recall the use of the more classic JDBC API and come up with the classic spring solution to JDBC: JdbcTemplate.

Yes, the JdbcTemplate solution uses the Java inner class, which is a good solution and we feel pretty good with it. But, as I said earlier in the Groovy explorer closure, Java internal classes are rather obscure and cumbersome to use. See here, the Java fans do not rush to throw smelly eggs for me, look at closure solution first.

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.