• 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. }

  • 相关阅读:
    Java 正则表达式
    高数 | 导数极限定理、分段点求导能不能用公式?导数和导数的极限?
    /run/udev/data 磁盘满
    Sulfo CY3-NH2生物分析和检测方法2183440-43-7星戈瑞
    实验九 数据微积分与方程数值求解(matlab)
    【应用统计学】几种常见的概率
    2000年数模B题 钢管订购和运输
    vue2中常用组件封装、全局挂载及使用
    苹果前工程师张晓浪承认窃取汽车商业机密,或被判10年监禁
    bignumber.js
  • 原文地址:https://blog.csdn.net/m0_54397364/article/details/126689500