0

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.

 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.

Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
krish
  • 11
  • 1
  • Simply call that print statement after calling `divideTwoNumbers`, in line 30? – Amongalen Mar 05 '20 at 09:52
  • 1
    Btw. You should use exception only for exceptional situations, someone entering 0 isn't one. You should handle it yourself with simple if else. – Amongalen Mar 05 '20 at 09:55
  • the `finally` cannot be executed after the `return` since the `return` terminates the method (and the other `println` is executed then). Also IMHO not the correct place to thank for using the application, that should the end of `main` (the real end of the application) - otherwise what would happen if the method get called two or more times? – user85421 Mar 05 '20 at 10:02
  • @user85421 So what should happen in the case where try and finally both have return statements ? – Trishul Singh Choudhary Mar 05 '20 at 10:04
  • @Trishul how is that related to what I wrote? And documentation states exactly what happens - " If the catch block completes abruptly for reason `R`, then the finally block is executed. ... If the finally block completes abruptly for reason `S`, then the try statement completes abruptly for reason `S` (and reason `R` is discarded)." (not to hard to test it yourself) (I also believe this question was already asked/answered here on StackOverflow) – user85421 Mar 05 '20 at 10:06
  • @Trishul check [Multiple returns: Which one sets the final return value?](https://stackoverflow.com/q/2309964/85421) (WOW 10 years old! Memory still working, *with a bit of search help* [: -) ) – user85421 Mar 05 '20 at 10:15

2 Answers2

0

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.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • No man,according to the problem I must use the finally block compulsory,that is the issue for me!!! – krish Mar 05 '20 at 13:46
  • "according to the problem" you didn't say there was a particular requirement to use `finally`. You just said you wanted the statement in the finally to execute after the the other one. – Andy Turner Mar 05 '20 at 15:09
0

Finally is always executed and before returning ans's value.

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

busetekin
  • 834
  • 7
  • 12