“This is the 15th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.
The vast sea of millions, thank you for this second you see here. I hope my article is helpful to you!
May you keep your love and go to the mountains and seas in the coming days!
Note: Yesterday we looked at some of the exception handling mechanisms, let’s continue to look at other forms of handling exceptions in Java, Java exception handling mechanism!
😮 Exception handling mechanism (key)
In Java applications, the exception handling mechanism is: throw an exception, catch an exception.
🟠try-finally form:
Try {// (try to run) program code}finally{// when an exception occurs, always execute code}Copy the code
Try-finally means that a piece of code will be executed regardless of its status.
Such as:
public class TryFinallyDemo { public static void main(String[] args) { int a = 10; int b = 0; try{ System.out.println(a / b); System.out.println(" try "); }finally{system.out.println (" will finally? "); } system.out.println (" out.out.println "); /* If there is no catch, only the finally statement will be issued and an exception will be raised. Will finally that the Exception in the thread "is the main" Java. Lang. ArithmeticException: / by zero at com.it.test3.TryFinallyDemo.main(TryFinallyDemo.java:8) */ } }Copy the code
You can see that the program is not working properly, and the finally block is still being executed.
🟡 try-catch-finally form:
Catch (exception type A e){// Catch and dispose of the exception type A} finally {// The block of statements that will be executed whether or not an exception occurs}Copy the code
Similar to try-finally, this means that code in finally will be passed regardless of the execution.
When an exception occurs ina method, the code after the exception will not be executed. If some local resources have been obtained before and need to be released, the code that releases local resources needs to be called both at the normal end of the method and in the catch statement, which is cumbersome. Finally statements can solve this problem.
Such as:
public class TryCatchFinallyDemo { public static void main(String[] args) { test(); } public static void test() { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { //date = simpleDateFormat.parse("2020-10-06"); Date = simpleDateformat.parse (" 06 Oct 2020 "); } catch (ParseException e) { e.printStackTrace(); }finally{system.out.println ("finally must be executed here "); } system.out. println(" will go out here "+ date); }}Copy the code
The result of running successful code:
Will you go outside here Tue Oct 06 00:00:00 CST 2020Copy the code
Results after running failed code:
java.text.ParseException: Unparseable date: "2020/10/06" at java.text.DateFormat.parse(DateFormat.java:366) at com.it.test3.TryCatchFinallyDemo.test(TryCatchFinallyDemo.java:19) at Com. It. Test3. TryCatchFinallyDemo. Main (TryCatchFinallyDemo. Java: 12) finally here will execution will go outside here is nullCopy the code
As you can see, the code for the finally statement block executes regardless of failure.
-
In try-catch-finally, if a return is returned ina catch, will finally be executed?
public class TryCatchFinallyDemo2 { public static void main(String[] args) { test(); } public static void test() { int a = 10; try{ System.out.println(a / 0); }catch(ArithmeticException e) { e.printStackTrace(); return ; }finally { System.out.println("finally"); }}}Copy the code
Running results:
java.lang.ArithmeticException: / by zero at com.it.test3.TryCatchFinallyDemo2.test(TryCatchFinallyDemo2.java:11) at com.it.test3.TryCatchFinallyDemo2.main(TryCatchFinallyDemo2.java:5) finally Copy the code
As you can see, finally is executed even if a return is returned ina catch.
So let’s think about finally before or after a return. We’ll answer that question tomorrow!
🌸 summary
I believe that you have a full understanding of the concept of exception handling mechanism, we think about it, have ideas can answer a wave of comments, tomorrow we continue to explain today’s remaining problems! Look forward to more in the next chapter. Welcome to the next chapter!
Let’s refuel together, too! I am not just, if there is any missing, wrong place, also welcome you to criticize in the comments of talent leaders! Of course, if this article is sure to help you a little, please kindly and lovely talent leaders to give a thumb-up, favorites, one key three even, thank you very much!
Here, the world is closed for today, good night! Although this article is over, I am still here, never finished. I will try to keep writing articles. The coming days are long, why fear the car yao ma slow!
Thank you all for seeing this! May you live up to your youth and have no regrets!
\