About Question enthuware.ocajp.i.v7.2.1172
Moderator: admin
-
- Posts: 23
- Joined: Fri Jun 20, 2014 3:12 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1172
When we run the program with no argument:
I understand very well that both "Exception in Main" and "The end" would get printed.
But why wouldn't we also get "Some Exception"?
Isn't that what the else throw new Exception("Some Exception"); supposed to do?
I understand very well that both "Exception in Main" and "The end" would get printed.
But why wouldn't we also get "Some Exception"?
Isn't that what the else throw new Exception("Some Exception"); supposed to do?
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1172
Not sure what you mean. Can you elaborate more on what exactly do you think throw statement does?
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Fri Jun 20, 2014 3:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1172
So we have
if you run the program with one arguments, then (args.length == 0) is false
so the else case is executed which is throw new Exception("Some Exception");
so the out put should be "Some Exception"
then the catch(Exception e) will catch the exception and System.out.println("Exception in Main");will be executed, which makes the "Some Exception" to be added to the out put
and then the finally, will make the "The end" ot be added to the out put.
Am I wrong?
Code: Select all
public class FinallyTest{
public static void main(String args[]){
try{
if (args.length == 0) return;
else throw new Exception("Some Exception");
}
catch(Exception e){
System.out.println("Exception in Main");
}
finally{
System.out.println("The end");
}
}
}
so the else case is executed which is throw new Exception("Some Exception");
so the out put should be "Some Exception"
then the catch(Exception e) will catch the exception and System.out.println("Exception in Main");will be executed, which makes the "Some Exception" to be added to the out put
and then the finally, will make the "The end" ot be added to the out put.
Am I wrong?
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1172
Why do you think throw new Exception("Some Exception"); should output "Some Exception"??so the else case is executed which is throw new Exception("Some Exception");
so the out put should be "Some Exception"
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Fri Jun 20, 2014 3:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1172
well because when I compile this
I get
Code: Select all
public class TestExp{
public static void main(String []args) throws Exception {
throw new Exception("Some Exception");
}
}
Compiling the source code....
$javac TestExp.java 2>&1
Executing the program....
$java -Xmx128M -Xms16M TestExp
Exception in thread "main" java.lang.Exception: Some Exception
at TestExp.main(TestExp.java:4)
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1172
The output is not printed by the statement "throws new Exception()". It is printed by the JVM because it caught the exception. "throw new Exception()" doesn't print anything. It merely throws the exception. Whoever catches it is free to either print it or not. Try this code:
HTH,
Paul.
Code: Select all
public class TestExp{
public static void main(String []args) {
try{
throw new Exception("Some Exception");
}catch(Exception e){
//here you have the Exception object referred to by e. You can choose to print it or do nothing with it.
//System.out.println(e);
//e.printStackTrace(); //this is what the JVM does if it catches the exception that is why you see the output.
}
}
}
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Fri Jun 20, 2014 3:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1172
Paul you are the best!!!
THANKS!
THANKS!
Who is online
Users browsing this forum: Bing [Bot] and 92 guests