There are generally two ways to handle exceptions, either catch the exception try-catch or throw the exception throws
If a method throws a runtime exception (throws RuntimeException) afterwards, the caller does not need to deal with it
If a method is thrown after a compile-time exception, the caller must handle it, either throw or try-catch;
Exceptions during the runtime are generally not handled, and are generally logical errors in the program, such as the denominator being 0 as the divisor. . .
Note that if an exception occurs in the try, the statement below the try will not be executed. Go back to find the catch matching exception processing and the next statement will be processed (that is, the statement after try-catch-finally will continue to be executed)
/*
* Sometimes, we can handle exceptions, but sometimes, we don't have permission to handle an exception at all.
* Or, if I can't handle it, I won't handle it.
* In order to solve the error problem, Java provides another solution for this situation: throwing.
*
* Format:
* throws exception class name
* Note: This format must be followed by brackets of the method.
*
* Notice:
* Try not to throw exceptions on the main method.
* But I did this for the sake of convenience.
*
* Summary:
* An exception is thrown during the compilation period and must be handled by the caller in the future.
* An exception is thrown during the runtime, and future calls are not required to be processed.
*/
Any exceptions of RuntimeException and its subclasses are not processed (not thrown or caught). If you really know that an exception will be thrown at runtime, then it is OK to directly check the logic of modifying the program! ! !
package exception;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class ExceptionDemo {public static void main(String[] args) {System.out.println("Before calling the method");try {method();// Exception throwing must be handled during compilation period} catch (ParseException e) {e.printStackTrace();}System.out.println("Call method 2 after calling the method 1");method2();}public static void method2() throws RuntimeException{int a = 10;int b = 0;System.out.println("a/b="+a/b);//Exceptions during runtime can be not processed. If the exception is thrown, the caller does not need to handle the exception}public static void method() throws ParseException {String string = "2015-05-30";SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//The string format will be gradually matched, and if the exception is not correct, the exception will be thrown SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//If string only has 2015-05-30 and there is no follow-up, then it will run an exception because this sdf2 cannot match HH:mm:ssDate date = sdf.parse(string);System.out.println("Date formatting:" + date);}}Exception structure diagram:
We programmers can't handle error exceptions without having to deal with them.
We don't need to deal with the runtime exception of the runtime exception.
The others are exceptions in the compilation period, and we have to deal with them.
The above is all the content that the editor has brought to you about the difference between abnormal structure diagram, compile period exception and run period exception. I hope it will be helpful to everyone and support Wulin.com more~