File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exception with returns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "exception with returns" Watch "exception with returns" New topic
Author

exception with returns

Hamraj Kulshreshtha
Ranch Hand

Joined: Oct 06, 2007
Posts: 31
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


SCJP 1.5 91%
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Feb 01, 2007
Posts: 102
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..


kaushik Vira
-------------------------------------
SCJP, Preparing SCWCD..
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
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.


Thanks and Regards
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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
 
subject: exception with returns
 
Similar Threads
core java
explain AssertionError
Modify by throwing
Rethrow an exception
Anonymous inner class and Exception Handling