exception return

gudii9
gudii9 used Ask the Experts™
on
class ExceptionReturn
{
public static void main(String[] args)
{
System.out.println(m1());
}
public static getName(){
try
{
System.out.println(10/0);
return 1;
}
catch(ArithmeticException e)
{
return 2;
}
finally{
return 3;
}
}}

Open in new window


i wonder why i got 3 as output not 1,2

class ExceptionReturn2 {
	
	public static void main(String[] args) {

		try {
			System.out.println("try");
			//System.exit(0);
			return;
			//System.exit(0);
		} catch (ArithmeticException e) {
			System.out.println("catch");
		} finally {
			System.out.println("finally");
		}
	}
}

Open in new window


how finally overriding return

System.exit(0); cannot be given after return?

please advise
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Java Architect
Commented:
If a finally block is present, it will always be executed. If you have a return statement inside a try block, the code in the finally is executed immediately before the method returns. If that block also has a return statement, that value is returned instead. That's why your first example returns 3 instead of 1 or 2.

In your second example, the code in the finally block is, again, executing immediately before the return statement is executed.
Commented:
The finally clause executes always, and after the catch if there's one.
The fact is that the first return executes (the finally return), and then the call stack goes up to the main.

System.exit(0) cuts the execution of the program and it's impossible to get into catch or finally.
Manikandan ThiagarajanSenior consultant
Commented:
finally class must be executed if exception occurs or not
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Commented:
stmani2005: that's not true in the case of System.exit(0).

public class ExceptionExample
{
        public static void main(String[] args)
        {
                try
                {
                        System.out.println("TRY");

                        System.exit(0); //return;
                }
                catch(Exception e)
                {
                        System.out.println(e);
                }
                finally
                {
                        System.out.println("FINALLY");
                }
        }
}

Open in new window


If you execute this code, you WON'T receive "FINALLY" on screen.

If you exchange System.exit(0) by return, then you WILL see "FINALLY" on screen.

You don't have to believe me. Try javac and java this code, and you will see that.
Manikandan ThiagarajanSenior consultant

Commented:
Sorry,i didnt noticed system.exit(0) in the code

Author

Commented:
If a finally block is present, it will always be executed. If you have a return statement inside a try block, the code in the finally is executed immediately before the method returns. If that block also has a return statement, that value is returned instead. That's why your first example returns 3 instead of 1 or 2.

actually i am getting some compilation error.


Exception in thread "main" java.lang.Error: Unresolved compilation problem:
      The method m1() is undefined for the type ExceptionReturn

      at simple.servlet.ExceptionReturn.main(ExceptionReturn.java:3)

please advise
CPColinSenior Java Architect

Commented:
I'm sure you've seen an error like that before. You're calling a method called "m1" and no such method exists.

Author

Commented:
class ExceptionReturn {
	public static void main(String[] args) {
		System.out.println(getName());
	}

	private static char[] m1() {
		// TODO Auto-generated method stub
		return null;
	}

	public static int getName() {
		try {
			System.out.println(10 / 0);
			return 1;
		} catch (ArithmeticException e) {
			return 2;
		} finally {
			return 3;
		}
	}
}

Open in new window


why above gave below output
3


not
1 2 3
CPColinSenior Java Architect

Commented:
Only one return statement can be executed per method call. Please reread the comments above to see why the one that executed was the one from the finally block.

Author

Commented:
Only one return statement can be executed per method call.
i do not know this rule.
java all about millions of rules which we should always remember in the head

Author

Commented:
If you exchange System.exit(0) by return, then you WILL see "FINALLY" on screen.

what is meaning of exchanging one with other here?

Author

Commented:
public class ExceptionExample
{
        public static void main(String[] args)
        {
                try
                {
                        System.out.println("TRY");

                        System.exit(0); //return;
                }
                catch(Exception e)
                {
                        System.out.println(e);
                }
                finally
                {
                        System.out.println("FINALLY");
                }
        }
}

Open in new window


above gave below output
TRY


public class ExceptionExample
{
        public static void main(String[] args)
        {
                try
                {
                        System.out.println("TRY");

                        //System.exit(0); //return;
                }
                catch(Exception e)
                {
                        System.out.println(e);
                }
                finally
                {
                        System.out.println("FINALLY");
                }
        }
}

Open in new window


above gave
TRY
FINALLY

Commented:
That's correct.

The usual behavior of try/catch/finally is the following:
  1. The try block is executed.
  2. If one line of the try block throws an Exception, then the flow of the code jumps the first catch block that has the Exception type that corresponds with that Exception.
  3. The finally block is executed always.

The exception to this flow is when you use the System.exit(0) function.
This breaks the flow of the program immediately and this prevents the finally block to be executed.

That's why if you substitute System.exit(0) by return, you see the results of the finally block, and when you use System.exit(0) you can't see the results of the finally block.
CPColinSenior Java Architect

Commented:
java all about millions of rules which we should always remember in the head

Think of it this way: You call System.out.println() only once, so it can only print one value, right?

Author

Commented:
That's why if you substitute System.exit(0) by return, you see the results of the finally block,

you mean comment line System.exit(0) ?

Commented:
Yes.

If you comment line System.exit(0), then there's nothing that can prevent the execution of the finally block.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial