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.