Misuse of Java exceptions

Source: Internet
Author: User

Misuse of exceptions is very common. I have already listed several exceptions in the previous section. Let's take a closer look. Let's talk about two other things.
Example 1. Catch all exceptions with an exception, which is quite "easy to understand. But this is also the most silly behavior.
Public void readfile (string file ){
Bufferedreader reader = NULL;
Connection conn = NULL;
Try {
Reader = new bufferedreader (New inputstreamreader (
New fileinputstream (File )));
// Do some other work

Conn = drivermanager. getconnection ("");
//...
} Catch (exception e ){
E. printstacktrace ();
} Finally {
Try {
Reader. Close ();
Conn. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}
}
From the exception perspective, this is strict. Program It is indeed foolproof, and all exceptions can be captured. But from the programmer's point of view, how can we tell if this program goes wrong? IO or JDBC... so, it is worth taking as a counterexample. Don't think that this approach is very naive, and a fool will do it. During my internship at the company, I did see a similar situation: people did not use exception but throwable.

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.