First, the treatment of abnormal second method
1.try......catch...
grammar:
try{ The code may appear abnormal; }catch{ The code that handles; }catch{
note:
(1) the introduction of anything unusual, catch which must be clearly written, there was nothing unusual how to do;
(2) may have abnormal parent and child classes, in this order from the capture; therefore needed when writing abnormality in top-down, from small to large (i.e. from a subclass to superclass exception )
(3) try ,,, catch up to execution .... a catch block, after the execution try ..... catch .... is over.
package com.bjpowernode.java_learning; import java.io. * ; public class D63_1_TryCatchExercise { public static void main(String[] args) { try { FileInputStream f1 = new FileInputStream("C:\\user"); f1.read(); }catch(ArithmeticException a) { }catch(FileNotFoundException f) { } } }
For throws exception processing to occur on the code block abnormality coverage, otherwise an error, for example: the reason is not processed read () method of introducing an IOException.
package com.bjpowernode.java_learning; import java.io. * ; public class D63_1_TryCatchExercise { public static void main(String[] args) throws FileNotFoundException{ FileInputStream f1 = new FileInputStream("C:\\user"); f1.read(); } }
Correct way is to change one line of code
public static void main(String[] args) throws FileNotFoundException,IOException
Two, getMessage method and printStackTrace
package com.bjpowernode.java_learning; import java.io. * ; public class D63_2_MethodOfgetMessageAndprintStackTrace { public static void main(String[] args) { try { FileInputStream f1 = new FileInputStream("C:\\fjdoa"); }catch (FileNotFoundException e) { // print exception stack information // will use this method to debug programs in general e.printStackTrace (); // The following methods and functions of this method is actually the same as above, but usually use the above method, since the above method can print out more detailed information String msg = e.getMessage (); System.out.println(msg); } System.out.println("ABC"); } }
Third, the source code:
D63_1_TryCatchExercise.java
D63_2_MethodOfgetMessageAndprintStackTrace.java
https://github.com/ruigege66/Java/blob/master/D63_1_TryCatchExercise.java
https://github.com/ruigege66/Java/blob/master/D63_2_MethodOfgetMessageAndprintStackTrace.java
2.CSDN:https://blog.csdn.net/weixin_44630050
3. Park blog: https: //www.cnblogs.com/ruigege0000/
4. Welcomes the focus on micro-channel public number: Fourier transform public personal number, only for learning exchanges, backstage reply "gifts" to get big data learning materials