background
The following describes ParseException as an example.
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class MyException { public static void main(String[] args){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse("2021-0829"); System.out.println("Execute subsequent code..."); } }
In this case, parse() will report an error, because to prevent ParseException, it belongs to compilation exception and must be solved with throws or try... catch.
Mode 1:
Let's start with throws:
Alt+Enter at parse and select Add exception to method signature. throws ParseException will be automatically added after main. The code changes to:
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class MyException { public static void main(String[] args) throws ParseException { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse("2021-0829"); System.out.println("Execute subsequent code..."); } }
To write with throws is to hand over the exception to the JVM (java virtual machine) for processing. The disadvantages of doing so are:
The console directly displays the abnormal information, terminates the program, and returns the exit code 1, indicating abnormal exit. Note that the following code will not execute.
Mode 2:
We use try... catch:
Alt+Enter at parse, select Surround with try/catch, and the code changes to:
public class MyException { public static void main(String[] args) { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); try { Date date = sdf.parse("2021-0829"); } catch (ParseException e) { e.printStackTrace(); } System.out.println("Execute subsequent code..."); }
In this case, an exception is displayed, and the following code will be executed. The exit code 0 is returned, indicating normal exit.
Now that we have these two ways to handle exceptions, let's look at several variants:
Variant 1: custom methods, all with throws
If the code that may cause exceptions is in the custom method, (two throws cannot be less) for example:
public class MyException { public static void main(String[] args) throws ParseException{ method(); System.out.println("Follow up execution main code..."); } public static void method() throws ParseException { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse("2021-0829"); System.out.println("Implementation follow-up method code..."); } }
The operation results are as follows. After analysis, this exception is thrown when entering the line of method() execution to Date, and then throws to the caller of the method, that is, to the main function for processing. However, the main function is also throws, and finally to the virtual machine for processing. The virtual machine handles it by displaying the exception and terminating the program, Then, all the tasks under the Date line are terminated. Neither the sout h in the method method method nor the South back to main() are executed.
Variant 2: custom methods, all using try... catch
In fact, you don't have to use try... catch. If you use both, there will be duplication. You can only delete try... catch in the main function.
In fact, there is only one try... catch:
public class MyException { public static void main(String[] args) { method(); System.out.println("Implementation follow-up main code..."); } public static void method() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = sdf.parse("2021-0829"); } catch (ParseException e) { e.printStackTrace(); } //Subsequent code System.out.println("Implementation follow-up method code..."); } }
What is the result of this situation? In the method method, after handling the exception, execute the code in catch {}, print the exception information, continue to execute the subsequent code of the method, then return to main, and execute the subsequent code of the main. Therefore, in this way, both subsequent codes can be executed.
Variant 3: custom method, throws in main, try... catch in method
The code is as follows:
public class MyException { public static void main(String[] args) throws ParseException{ method(); System.out.println("Implementation follow-up main code..."); } public static void method() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = sdf.parse("2021-0829"); } catch (ParseException e) { e.printStackTrace(); } //Subsequent code System.out.println("Implementation follow-up method code..."); } }
As a result of this situation, the steps are basically the same as variant 2. In the method method, after handling the exception, print the exception output, execute the subsequent code of the method, and then return to main. Note that the method method does not transfer the exception to main for processing, but handles it by itself, (equivalent to that the son did something wrong and didn't tell his father), so the throws in main didn't catch an exception, so both subsequent codes can be executed.
Think again. Is the above exception message try... catch in method or throws in main? The answer must be try... catch in method. If you don't believe it, we'll use e.printStackTrace() in the method method; Replace with system out. println(e); Let's look at the results:
Variant 4: custom method, try... catch in main, throws in method
This variant is interesting. The code is as follows:
public class MyException { public static void main(String[] args){ try { method(); } catch (ParseException e) { e.printStackTrace(); } System.out.println("Implementation follow-up main code..."); } public static void method() throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse("2021-0829"); //Subsequent code System.out.println("Implementation follow-up method code..."); } }
In this case, an exception in the method is caught by throws and cannot be handled by itself, resulting in the method itself being terminated and passed to main for processing. Then, the try... catch of the main function will handle the exception and print the exception information. Then, the subsequent code of main continues to be executed, the exit code is 0, and the program exits normally. (it's equivalent to that the son got into trouble and went to the Bureau. He can't do anything by himself. Then the father learned that he can clean up the mess for his son and do other things)
Note here that this exception is printed out by the main function. If you don't believe it, we put e.printStackTrace() of the main function; Replace with system out. println(e); Then look at the results:
Summary:
The above discussion takes ParseException as an example. It is a compilation exception and a problem in the program during compilation. It must be solved with throws or try... catch.
1. If throws is used, the exception will be handled by the caller of the method (it will not be handled by yourself, but will be specially transferred to others). Then you can transfer it to me, and finally it will be handed over to the java virtual machine for processing, and then the program will be terminated, and the subsequent code will not be executed.
2. If try... catch is used, it will be handled by yourself. If it is not handed over to the java virtual machine, the subsequent code will also be executed.
Therefore, in our work, we generally adopt variant 4, that is, the son throws up in trouble and gives it to the outermost father to try... catch. In this way, we can not only know the problem (the son's actions are not executed, so it's easy to see the problem), but also execute the follow-up code of main and exit normally. Why not!