File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exception with returns

 
Hamraj Kulshreshtha
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please give the explanation why the output is not coming as "ab" in this code??
Instead the code is running without any output.

class ExceptionTest
{
public static void main(String args[]) throws Exception
{
try
{
throw new Exception("a");
}
catch(Exception e)
{
throw new Exception("b");
}
finally
{
return;
}
}
}
Regards,
Hamraj
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy "Hamraj Happy",

Can you please kindly adjust your display name according to the ranch's naming policy?

You can easily do so by editing your profile.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by hamraj happy:
Please give the explanation why the output is not coming as "ab" in this code??
Instead the code is running without any output.



Where are you printing in your code so that your expected output gets displayed?

Moreover, you are throwing an exception and again rethrowing it in the catch block. All what you have in the final block is a single return statement which does nothing but returns the control back to the place of calling.

Try having some meaningful System.out.println() statements in each block. You will get to know.
 
kaushik vira
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you are only throwing exception, and in finally you put return; so you are even not getting runtime exception (which you are pragmatically throwing).

And even you not putted any sop. so how can you get any output..
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by hamraj happy:
Please give the explanation why the output is not coming as "ab" in this code??
Instead the code is running without any output.

class ExceptionTest
{
public static void main(String args[]) throws Exception
{
try
{
throw new Exception("a");
}
catch(Exception e)
{
throw new Exception("b");
}
finally
{
return;
}
}
}
Regards,
Hamraj


Are you confused because you are not getting an exception stack trace? If yes, its because the finally block has eaten up the exception you raised due to the "return" statement. Comment out the return; to see the stack trace.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satya Maheshwari:


Are you confused because you are not getting an exception stack trace? If yes, its because the finally block has eaten up the exception you raised due to the "return" statement. Comment out the return; to see the stack trace.


No. I guess he is concerned of NOT getting the output.


Please give the explanation why the output is not coming as "ab" in this code??
Instead the code is running without any output.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic