• finally执行语句的注意和小陷阱


    1. public class FinallyTest {
    2. public static void main(String[] args) {
    3. int res=div(10,0);
    4. System.out.println(res);
    5. }
    6. public static int div(int a,int b){
    7. int num=0;
    8. try {
    9. num=a/b;
    10. }catch (ArithmeticException e){
    11. System.out.println("算数异常");
    12. }catch (NumberFormatException e){
    13. e.printStackTrace();
    14. }catch (Exception e){
    15. System.out.println("其他异常");
    16. }finally {
    17. System.out.println("一定执行的语句");
    18. }
    19. return num;
    20. }
    21. }

    1. public class FinallyTest {
    2. public static void main(String[] args) {
    3. int res=div(10,0);
    4. System.out.println(res);
    5. }
    6. public static int div(int a,int b){
    7. int num=0;
    8. try {
    9. num=a/b;
    10. }catch (ArithmeticException e){
    11. System.out.println("算数异常");
    12. return 11;
    13. }catch (NumberFormatException e){
    14. e.printStackTrace();
    15. }catch (Exception e){
    16. System.out.println("其他异常");
    17. }finally {
    18. System.out.println("一定执行的语句");
    19. }
    20. return num;
    21. }
    22. }

     

    结论:

    finally 是可选的,一旦写上就一定执行的语句,即使过程中有return也一定执行

    1. /*
    2. finally 是可选的,一旦写上就一定执行的语句,即使过程中有return也一定执行
    3. */
    4. public class FinallyTest {
    5. public static void main(String[] args) {
    6. int res=div(10,0);
    7. System.out.println(res);
    8. }
    9. public static int div(int a,int b){
    10. int num=0;
    11. try {
    12. num=a/b;
    13. }catch (ArithmeticException e){
    14. System.out.println("算数异常");
    15. return 11;
    16. }catch (NumberFormatException e){
    17. e.printStackTrace();
    18. }catch (Exception e){
    19. System.out.println("其他异常");
    20. }finally {
    21. System.out.println("一定执行的语句");
    22. return 22;
    23. }
    24. }
    25. }

     

    1. /*
    2. finally 是可选的,一旦写上就一定执行的语句,即使过程中有return也一定执行
    3. */
    4. public class FinallyTest {
    5. public static void main(String[] args) {
    6. int res=div(10,0);
    7. System.out.println(res);
    8. }
    9. public static int div(int a,int b){
    10. int num=0;
    11. try {
    12. num=a/b;
    13. }catch (ArithmeticException e){
    14. System.out.println("算数异常");
    15. return num;
    16. }catch (NumberFormatException e){
    17. e.printStackTrace();
    18. }catch (Exception e){
    19. System.out.println("其他异常");
    20. }finally {
    21. num+=100;
    22. System.out.println("一定执行的语句");
    23. }
    24. return num;
    25. }
    26. }

    1. /*
    2. finally 是可选的,一旦写上就一定执行的语句,即使过程中有return也一定执行
    3. */
    4. public class FinallyTest {
    5. public static void main(String[] args) {
    6. int res=div(10,0);
    7. System.out.println(res);
    8. }
    9. public static int div(int a,int b){
    10. int num=0;
    11. try {
    12. num=a/b;
    13. }catch (ArithmeticException e){
    14. System.out.println("算数异常");
    15. return num;
    16. }catch (NumberFormatException e){
    17. e.printStackTrace();
    18. }catch (Exception e){
    19. System.out.println("其他异常");
    20. }finally {
    21. num+=100;
    22. System.out.println("一定执行的语句");
    23. return num;
    24. }
    25. }
    26. }

  • 相关阅读:
    使用VBA实现快速模糊查询数据
    Python中的并发编程是什么,如何使用Python进行并发编程?
    Linux------权限篇1)
    [Go版]设计模式——Template模版方法模式
    51单片机定时器中断
    基于SpringBooy的安康旅游网站的设计与实现
    云原生Docker Cgroups资源控制操作
    一个优质软件测试工程师简历,疯狂面试5家公司......
    Python MQTT客户端 paho-mqtt
    JVM 第一部分 JVM两种解释器 类加载过程和类加载器
  • 原文地址:https://blog.csdn.net/m0_54397364/article/details/126689500