aspose file tools*
The moose likes Java in General and the fly likes print StackTrace in finally block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "print StackTrace in finally block" Watch "print StackTrace in finally block" New topic
Author

print StackTrace in finally block

Nishita Jain
Ranch Hand

Joined: Mar 30, 2006
Posts: 97
hi,
We normally use catch block to print StackTrace.
e.g.
try{
}
catch(Exception e){
e.printStackTrace();
}
But I want to print StackTrace in finally block without using catch block . can anyone guide me about this?
Thanks in advance,
Nishita


Nishita
gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
i had one question, why you want this?
this can be done, declare a variable of type Exception which should be accessible to both catch and finally blocks(visibility),
when you catch the exception, assign the exception to that variable, and then in finally call the method,
but put a check for null in case exception is not thrown..

but tell me why you need this??


thanks,<br />gaurav abbi
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3223
Beware that the finally block is executed even if there is no exception. So why would you want to print the stack trace when there is no stack trace


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344


Originally posted by Nishita Jain:
But I want to print StackTrace in finally block without using catch block . can anyone guide me about this?


Why would you really want that? How can you get the Exception in finally block without catching it? I dont think it makes sense as the exception object would not be available or passed into finally block directly.

If i m not on track, kindly correct me.

Perhaps, this thread may give you a good information on this. Please look at the answer given by Carl Trusiak especially!.

HtH.
[ June 21, 2007: Message edited by: Raghavan Muthu ]

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]
Nishita Jain
Ranch Hand

Joined: Mar 30, 2006
Posts: 97
Thanks everyone for replies.
You all have same question , why i want that. Actually this question was asked in my interview.
Gaurav said its possible but he has used Catch block. so what if we remove catch block? can we access that stack in finally without using catch?
I think Raghvan said its not possible..

thnaks
Nishita
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
No way.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344


Originally posted by Nishita Jain:
so what if we remove catch block? can we access that stack in finally without using catch?


Just think of it. In a programming environment, how come it would ever be possible if you dont have the required exception caught at some point or place {as what gaurav said - but that is possible ONLY if you get it in CATCH block! - but your question is violating that!)?

Unless and until Java does provide some Magic wherein you can get an object or value available without getting/storing it manually and/or passing it to the called place.

In case of traditional try/catch block, the JRE does the job of passing the Exception object to you. That too in catch block NOT in finally block.

Otherwise, the syntax for try/catch/finally block would have been



Aint I?


HtH.

[ June 21, 2007: Message edited by: Raghavan Muthu ]
[ June 21, 2007: Message edited by: Raghavan Muthu ]
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
As Raghavan Muthu pointed out , it doesn't make much sense.

Even if you make the exception object available , you have to check whether a exception has occurred or not as finally would always be executed .It would not be a good idea as this will not give you any advantage.



Does this help you in anyway ?



Rahul Bhattacharjee
LinkedIn - Blog
Nishita Jain
Ranch Hand

Joined: Mar 30, 2006
Posts: 97
Thanks everyone !
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Ok, thoroughly contrived ...

Ugly enough for you?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hello Stan James

Thats right. But in your example, you dont throw an exception. In such case, why anyone would really want to have the code surrounded by try block?
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Raghavan Muthu:
Thats right. But in your example, you dont throw an exception. In such case, why anyone would really want to have the code surrounded by try block?



Stan has just given you a possible code without Catch clause because,

Originally posted by Nishita Jain:
So what if we remove catch block? can we access that stack in finally without using catch?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Thats fine Adeel Ansari. I understood what he said.

But still, the whole scenario would be meaningless! Aint I? The same holds good for the OP also, which i had mentioned in my previous post as well.
Don Morgan
Ranch Hand

Joined: Jul 24, 2003
Posts: 84
It is possible to just print the stack trace without explicitly throwing an exception at all, (which is sometimes useful for debugging). This can be placed within a finally block too.


[ June 21, 2007: Message edited by: Don Morgan ]

Don Morgan, Founder
www.DeveloperAdvantage.com - FREE Audiobooks for Software Developers
gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
well
i really hate those senseless interview questions far away from any programming strategy or coding style, they just want the interviewee to respond with the proper reasoning.
anyways nishita, what was your response?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hello Don,


Thread.currentThread().dumpStack();



Thanks for the additional info
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Or better use Thread.dumpStack();

But then again it would be of no use. Creating an exception and printing a stack trace doesn't sounds right.
[ June 22, 2007: Message edited by: Anupam Sinha ]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hey Anupam,


Creating an exception and printing a stack trace doesn't sounds right.


thats what i had been telling
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
But I want to print StackTrace in finally block without using catch block

My sample accomplished this and nothing else. It's absolutely not sane or useful code. As I pointed out, you could throw e in the try block which would make it semi-useful but still not a good idea. We could even initialize e to null, create the exception in the try block and print stack trace only if e is not null.

I also agree that trick questions like this are not very useful. It might be fun to see how interviewees respond to the pressure, but I'd probably respond by rolling my eyes and checking my watch.
[ June 22, 2007: Message edited by: Stan James ]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344


I also agree that trick questions like this are not very useful. It might be fun to see how interviewees respond to the pressure, but I'd probably respond by rolling my eyes and checking my watch.


My Imagination on the scene was too good Stan James

But, you are right! At times, these situations occur just for the sake of it atleast.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: print StackTrace in finally block