• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

exception with returns

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 love a good mentalist. And so does this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic