简体   繁体   English

如何在try和catch块的return语句之后打印finally块中的语句?

[英]how to get the statement in finally block printed after the return statement from try and catch block?

I want the statement in the finally block to be printed after the return statement of try and catch block , but the statement in finally block always prints before this.我希望finally块中的语句在try and catch block的 return 语句之后打印,但 finally 块中的语句总是在此之前打印。

 1 import java.io.*;
    2 import java.util.*;
    3 public class Division
    4 {
    5     public String divideTwoNumbers(int number1,int number2)
    6     {
    7         try
    8         {
    9         int n=number1/number2;
   10         String ans="The answer is "+n+".";
   11         return ans;
   12         
   13         }
   14         catch(ArithmeticException e)
   15         {
   16             String s1="Division by zero is not possible. ";
   17              return s1;
   18         }
   19         finally
   20         {
   21             System.out.print("Thanks for using the application");
   22         }
   23     }
   24     public static void main(String[] args)
   25     {
   26         Division obj=new Division();
   27         Scanner sc=new Scanner(System.in);
   28         System.out.println("Enter the numbers");
   29         System.out.println(obj.divideTwoNumbers(sc.nextInt(),sc.nextInt()));
   30     }
   31 }

For input:对于输入:

`15` and `0`

output needed:需要的输出:

`Division by zero is not possible. Thanks for using the application.`

the output I am getting:我得到的输出:

Thanks for using the application. Division by zero is not possible.

If you always want the message to be shown after printing the result of the method call, print it after the method call:如果您总是希望在打印方法调用的结果后显示消息,请在方法调用后打印:

System.out.println(obj.divideTwoNumbers(sc.nextInt(),sc.nextInt()));
System.out.println("Thanks for using the application");

And remove the finally .并删除finally

Finally is always executed and before returning ans's value.最后总是在返回 ans 的值之前执行。

import java.io.*;
import java.util.*;
public class Division {
    public String divideTwoNumbers(int number1, int number2) {
        try {
            int n = number1 / number2;
            String ans = "The answer is " + n + ".";
            return ans;

        } catch (ArithmeticException e) {
            String s1 = "Division by zero is not possible. ";
            return s1;
        }

    }

    public static void main(String[] args) {
        try {
            Division obj = new Division();
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter the numbers");
            System.out.println(obj.divideTwoNumbers(sc.nextInt(), sc.nextInt()));
        }

        finally {
            System.out.print("Thanks for using the application");
        }
    }
}

Output: Enter the numbers 15 3 The answer is 5. Thanks for using the application输出:输入数字 15 3 答案是 5。感谢使用该应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM