Can Some one explain to me why the output will be like this...
Code :
public class Test {
static String s = "One ";
public static void main(String[] args) {
new Test().method1();
System.out.println(s);
}
static void method1(){
try{
method2();
}
catch(Exception e){
s += "Two ";
}
}
static void method2() throws Exception{
s += "Three ";
method3();
s += "Four ";
method3();
s += "Five ";
}
static void method3() throws Exception{
s += "Six ";
throw new Exception();
}
}
Why the output of this is not == > One Three Six Four Six Five
Can someone explain what exception is occur and where it occur??
Re: Can Some one explain to me why the output will be like this...
Please copy and paste here the full contents of the command prompt window that shows the programs output and errors etc.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
Re: Can Some one explain to me why the output will be like this...
All the exceptions have been caught by the exception handle and the output for this coding is "One Three Six Two"... And I don't know how to use the command prompt to execute this coding. I'm using JCreator to execute. I wish someone can explain to me why the output is "One Three Six Two" instead of "One Three Six Four Six Five"... Anyway thanks for your reply. Much appreciated...
Re: Can Some one explain to me why the output will be like this...
Quote:
explain to me why the output is
Add lots printlns to the code to show where the execution flow goes. Put one after every statement.
The exception that is thrown changes the execution flow.
Re: Can Some one explain to me why the output will be like this...
I did tried to put a println after every statement but I wondering what kind of exception occur and changed the flow of execution. Any thing wrong with the way for this coding to call its function?
And I tried to use command prompt to run it. There are no exception and this is the out put
C:\Users\lun\Desktop>java Test
One Three Six Two
C:\Users\lun\Desktop>
Re: Can Some one explain to me why the output will be like this...
Can I ask why you're throwing an Exception inside method3.
I removed the line
throw new Exception();
and it outputted
One Three Six Four Six Five
The way you told it to go, it seemed to be going into the catch block as you just told it to throw an Exception in method3 which was called by method2 which was called in the try block.
Re: Can Some one explain to me why the output will be like this...
Actually this is a question from my past year examination question and I'm doing my revision. The question is to trace the output. Since the output I traced doesn't same as the output after i execute it then i post here for someone to explain to me :(
Re: Can Some one explain to me why the output will be like this...
Thanks to Norm and javapegiun. I think I'm understood why the output will be like this.