0
public class UsingExceptions {

    public static void main(String[] args) {
        try{
            throwException();
        }
        catch(Exception e){
            System.err.println("Exception handled in main");
        }
        doesNotThrowException();
    }

    public static void throwException()throws Exception{
        try{
            System.out.println("Method throwException");
            throw new Exception();
        }
        catch(Exception e){
            System.err.println("Exception Handled in Method throwExceptioin ");
            throw e;
        }
        finally{
            System.err.println("Finally executed in method throwException");
        }
    }

    public static void doesNotThrowException(){
        try{
            System.out.println("Method doesNotThrowException");
        }
        catch(Exception e){
            System.err.println("Exception handled in method doesNotThrowException");
        }
        finally{
            System.out.println("finally executed in doesNotThrowException");
        }
        System.out.println("end of method doesNotThrowException");
    }
}

My IDE is outputting:

  • Method throwException
  • Exception Handled in Method throwExceptioin
  • Method doesNotThrowException
  • Finally executed in method throwException
  • Exception handled in main
  • finally executed in doesNotThrowException
  • end of method doesNotThrowException

but I'm expecting:

  • Method throwException
  • Exception handled in method throwException
  • Finally executed in throwException
  • Exception handled in main
  • Method doesNotThrowException
  • Finally executed in doesNotThrowException
  • End of method doesNotThrowException

Where is my problem?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
SaS
  • 3
  • 1

1 Answers1

0

There are multiple outputs of the code.

Compiling with eclipse:

Exception Handled in Method throwExceptioin 
Method throwException
Finally executed in method throwException
Exception handled in main
Method doesNotThrowException
finally executed in doesNotThrowException
end of method doesNotThrowException

Compiling with standard javac compiler via Windows cmd.exe:

Method throwException
Exception Handled in Method throwExceptioin
Finally executed in method throwException
Exception handled in main
Method doesNotThrowException
finally executed in doesNotThrowException
end of method doesNotThrowException

Compiling with IntelliJ IDEA:

Method throwException
Method doesNotThrowException
finally executed in doesNotThrowException
end of method doesNotThrowException
Exception Handled in Method throwExceptioin 
Finally executed in method throwException
Exception handled in main

As user Sorin statet. IDEs handle printing to the console differently. IntelliJ IDEA first outputs all the System.out.println() and then all System.err.println(). The standard java compiler threads them in expected order and eclipse in some kind of weird order.

If you want the code you expected in every IDE you would have to use just the System.out.println() or just the System.err.println(). You could also take a look at this answer

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Lasnik
  • 256
  • 2
  • 11
  • While your helpful intent is acknowledged, this is not an answer to the question. It's also because the OP uses both System.out and System.err which IDE's (at least IntelliJ) handle at their own sequence. – Sorin Mar 17 '21 at 20:51
  • 1
    I would write a comment but I am not able to comment under other people posts (because of my reputation) but I still want to post the information I have. – Lasnik Mar 17 '21 at 20:53