Java Exception handling practices

Source: Internet
Author: User
Tags exception handling
Do you think you are a Java expert? Are you sure you have fully mastered the Java Exception handling mechanism? In the following code, can you quickly find the six problems for exception handling?
1 OutputStreamWriter out =...
2 java. SQL. Connection conn =...
3 try {// retry
4 Statement stat = conn. createStatement ();
5 ResultSet rs = stat.exe cuteQuery (
6 "select uid, name from user ");
7 while (rs. next ())
8 {
9 out. println ("ID:" + rs. getString ("uid") // response
10 ", name:" + rs. getString ("name "));
11}
12 conn. close (); // (3)
13 out. close ();
14}
15 catch (Exception ex) // (2)
16 {
17 ex. printStackTrace (); // (1), (4)
18}
As a Java programmer, you should be able to identify at least two problems. However, if you cannot find all six questions, read this article.
This article does not discuss general principles of Java exception handling, because these principles are well known to most people. What we need to do is to analyze common bad habits that can be called "anti-pattern" (anti-pattern) against excellent coding specifications and help readers familiarize themselves with these typical negative examples, in this way, you can perceive and avoid these problems keenly in your actual work.
Counterexample: discard exception
Code: lines 15-18.
This code captures exceptions without any processing. It can be regarded as a killer in Java programming. In terms of the frequency and severity of the problem, it may be comparable to that of the C/C ++ program ?? Do not check whether the buffer zone is full. If you see this discard (rather than throw) exception, you can be 99% sure that the code is faulty (in rare cases, this code has a reason, but it is best to add a complete comment to avoid misunderstanding ).
The error in this code is that exceptions (almost) always mean something is wrong, or at least something unusual happens, we should not remain silent or indifferent to the distress signals sent by the program. Calling printStackTrace does not count as "exception handling ". Yes. Calling printStackTrace is helpful for debugging programs. However, after the program debugging stage ends, printStackTrace should no longer take the primary responsibility in the exception handling module.
Discard exceptions are common. Open the JDK ThreadDeath class document and you can see the following description: "In particular, although ThreadDeath is a 'normal Situation ', the ThreadDeath class is a subclass of Error rather than Exception, because many applications capture all exceptions and discard them." This section indicates that, although ThreadDeath represents a common problem, many applications attempt to capture all exceptions and then do not handle them properly, therefore, JDK defines ThreadDeath as a subclass of Error, because the Error class represents a serious problem that general applications should not capture. It can be seen that the habit of dropping exceptions is so common that it has even affected Java's design.

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.