Suddenly, I was interested in exceptions. I ran a very simple program and found a problem that I ignored all the time. Although it didn't make any sense for this question, it was very important, the consequence would be terrible if we didn't figure out these things when using jdbc, io, and so on.
Next, let's talk about the concept of why I am wrong for the program. I just want to remind myself of it.
Package com. zhao. practice801;
02 /**
03 * Basic Study on exceptions
04 * <A class = referer href = "http://my.oschina.net/arthor" target = _ blank minmax_bound = "true"> @ author </A> Administrator
05 *
06 */
07 public class TestException {
08 public static void main (String [] args ){
09 String str = null;
10 // obtain the string length
11 int length = 0;
12 try {
13 length = str. length ();
14 System. out. println ();
15} catch (Exception e ){
16 e. printStackTrace ();
17}
18 System. out. println ("program exited ");
19}
20}
I always thought that if an exception occurs, I would not execute the following statement. I found the error. It is only a method body and will not overwrite the Code except the method body. This is the result of the program.
Exit Program
2 java. lang. NullPointerException
3 at com. zhao. practice801.TestException. main (TestException. java: 13)
,