Introduction to the author
Author name: Ming Shiyin in programming world
Introduction: CSDN blog expert has been engaged in software development for many years and is proficient in Java and JavaScript. Bloggers also learn and grow step by step from scratch, know the importance of learning and accumulation, and like to fight and upgrade with ADC. Welcome to pay attention and look forward to learning, growing and taking off with you!
introduction
Many Java beginners ask me that it's worrying for the novice to turn around and forget the Java knowledge he has studied very hard. How can Xiaobai grow up quickly and become a big cow?
In fact, there is only one skill to become a big God: "learn more and practice more", so brother Ming sorted out typical practice examples. Through practice, you can quickly improve coding skills and proficiency, so that you can never return on the way to become a big man (remember to practice with your own hands)!
Navigation
✪ introduction to Java white 200 case series directory index
◄ previous article 99. Common Java exceptions and handling
► next article to be updated
summary
In actual development, according to the execution process of try catch statement, try statement block and catch statement block may not be fully executed, while some processing codes are required to be executed.
In order to ensure that the physical resources opened in the try block can be recycled, the exception handling mechanism provides a finally code block.
The syntax format is as follows:
try {
//Statements with possible exceptions
} catch(ExceptionType e) {
//Handling exception statements
} finally {
//Clean up code blocks
}
We know that try catch can be matched:
try {
//Statements with possible exceptions
} catch(ExceptionType e) {
//Handling exception statements
}
finally statement can also be matched with try statement:
try {
//Logical code block
} finally {
//Clean up code blocks
}
Example 1
try catch statement
package demo.demo100; public class Exception1 { public static void main(String[] args) { int a = 0; try { a = Integer.parseInt("abc"); } catch (Exception e) { System.out.println("An exception was caught, but the program is running normally"); } System.out.println("a==="+a); } }
function:
An exception was caught, but the program is running normally
a===0
Example 2
try finally statement
package demo.demo100; public class Exception2 { public static void main(String[] args) { int a = 0; try { a = Integer.parseInt("abc"); }finally { System.out.println("a==="+a); } } }
Operation results:
From here, we can see that finally will be executed, no matter whether the exception occurs or not; Finally statements are executed even if exceptions occur.
Example 3
Execution order in try catch finally structure
package demo.demo100; public class Exception2 { public static void main(String[] args) { int a = 0; try { System.out.println("implement try"); a = Integer.parseInt("abc"); }catch (Exception e) { System.out.println("implement catch"); }finally { System.out.println("implement finally"); } } }
Operation results:
Execute try
Execute catch
Execute finally
Therefore, the execution order is try - > catch - > finally. If try does not trigger an exception, the execution order is try finally.
Example 4
If there are return values in try catch finally, what is the return result?
package demo.demo100; public class Exception3 { public static String test(){ int a = 0; try { a = Integer.parseInt("abc"); return "try"; }catch (Exception e) { return "catch"; }finally { return "finally"; } } public static void main(String[] args) { System.out.println(test()); } }
Operation results:
finally
You can see that when there is a return statement in finally, the final return is the return value in finally.
Example 5
When finally does not return a value, it returns the value in try or catch, depending on where it is executed.
package demo.demo100; public class Exception3 { public static String test(){ int a = 0; try { a = Integer.parseInt("abc"); return "try"; }catch (Exception e) { return "catch"; }finally { // return "finally"; } } public static void main(String[] args) { System.out.println(test()); } }
Operation results
catch
Because of an exception, the catch module is executed.
package demo.demo100; public class Exception3 { public static String test(){ int a = 0; try { //a = Integer.parseInt("abc"); return "try"; }catch (Exception e) { return "catch"; }finally { // return "finally"; } } public static void main(String[] args) { System.out.println(test()); } }
result
try
try was returned because there was no exception
Example 6
When a try or catch returns, but the return value is modified in finally, what is the return value?
package demo.demo100; public class Exception4 { public static String test(){ String res=""; int a = 0; try { //a = Integer.parseInt("abc"); res = "try"; return res; }catch (Exception e) { res = "catch"; return res; }finally { res = "finally"; System.out.println("res The final value of is:"+res); } } public static void main(String[] args) { System.out.println(test()); } }
Operation results:
The final value of res is finally
try
Although the value of res has been modified in finally, the return value of this time has been cached when returning in try, so the returned result is the result of running in try.
Example 7
For the use of multiple catches, if the exceptions in multiple catch blocks have inheritance relationship, the parent exception catch block is placed at the bottom.
package demo.demo100; public class Exception5 { public static void main(String[] args) { int a = 0; try { System.out.println("implement try"); a = Integer.parseInt("abc"); }catch (NumberFormatException e) { //e.printStackTrace(); System.out.println("implement NumberFormatException catch"); }catch (Exception e) { System.out.println("implement Exception catch"); }finally { System.out.println("implement finally"); } } }
Operation results:
Execute try
Execute NumberFormatException catch
Execute finally
Example 8
Modify the above example. If a larger exception is thrown in the try, it will be caught by the next catch.
package demo.demo100; public class Exception5 { public static void main(String[] args) { int a = 0; try { System.out.println("implement try"); //a = Integer.parseInt("abc"); throw new Exception("Throw a test exception"); }catch (NumberFormatException e) { //e.printStackTrace(); System.out.println("implement NumberFormatException catch"); }catch (Exception e) { System.out.println("implement Exception catch"); }finally { System.out.println("implement finally"); } } }
Operation results:
Execute try
Execute Exception catch
Execute finally
Example 9
Multiple nested uses of try catch, and exceptions are caught in the inner layer.
package demo.demo100; public class Exception6 { public static void main(String[] args) { int a = 0; try { try { System.out.println("implement try"); a = Integer.parseInt("abc"); }catch (NumberFormatException e) { System.out.println("Inner layer try-catch Exception caught"); } }catch (Exception e) { System.out.println("Outer layer try-catch Exception caught"); }finally { System.out.println("implement finally"); } } }
Operation results:
Execute try
Exception caught by inner try catch
Execute finally
Example 10
Multiple nested use of try catch caught exceptions.
package demo.demo100; public class Exception6 { public static void main(String[] args) { int a = 0; try { try { System.out.println("implement try"); //a = Integer.parseInt("abc"); throw new Exception("Throw a test exception"); }catch (NumberFormatException e) { System.out.println("Inner layer try-catch Exception caught"); } }catch (Exception e) { System.out.println("Outer layer try-catch Exception caught"); }finally { System.out.println("implement finally"); } } }
Operation results:
Execute try
Exception caught by outer try catch
Execute finally
Summary
This section summarizes the "try catch finally statement". I hope it can be helpful to you. Please help [like] + [collection] + [punch in the comment area]. If you are interested in learning Java with brother Xiao Ming, [pay attention to a wave] won't get lost.
Let me know you by punching in the comment area. Mingge will continue to pay attention to your learning progress!
Navigation
✪ introduction to Java white 200 case series directory index
◄ previous article 99. Common Java exceptions and handling
► next article to be updated
Popular column recommendation
1.Java game series (Tetris, aircraft war, plant war, zombie, etc.)
2.JavaWeb project practice (library management, online examination, dormitory management system, etc.)
3. Wonderful examples of JavaScript (aircraft war, minesweeping, snake eating, verification code, etc.)
4. Introduction to Java Xiaobai 200 cases
5. Learn Java from zero, learn Java with interest, and learn Java from the perspective of King glory