• 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 again....

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


the out put is:

exception caught
finally
0

where goes the exception in line 1 ? how come compiler eats it up? is there any such case where this happens?
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
u can catch the exception ocurring in catch my using another try catch inside the catch.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's right, exceptions thrown inside a catch block are 'eaten'
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
exceptions thrown inside a catch block are 'eaten'
u r not taking care of the exceptions raise in catch block.....
am i right
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since the finally clause should execute no matter whether a new exception
is thrown inside a catch block (or)Not the o/p "finally" is printed and

The "return 0" statement in finally clause actually nullifies the exception
so only you are not get the exception stack trace printed during runtime.

try commenting the return statement in finally block.


When ever there is a return statement inside a finally block then it will
result in a warning like

Ex.java:21: warning: finally clause cannot complete normally
}
^
1 warning
 
nils appeldoorn
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by alapati kirankumar:
exceptions thrown inside a catch block are 'eaten'
u r not taking care of the exceptions raise in catch block.....

you can take care of them, ofcourse! to be more specific you should, but I was just explaining wehy you didn't see the other exception, the one from 'line 1'

if you do it properly, you should create a chain of catched exceptions, with the most specific type first. you should, in my opinion, never run code inside a catch or finally that will cause an error ...
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vishnu prakash:

The "return 0" statement in finally clause actually nullifies the exception
so only you are not get the exception stack trace printed during runtime.


ya i have not heard about this in K&B .. new pt to remember..
ya this is true when i throw the checked exception too..

but what if i have to catch the expetion thrown in catch block how do i will do
should i declare another try/catch block pair inside catch block or will it be caught by next catch block assotiated with super try block ?
pls confirm about this...
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
finally has the final say over the exception thrown from a catch block.if it is not thrown from the finally block,the exception is simply discarded.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Shweta]: finally has the final say over the exception thrown from a catch block.if it is not thrown from the finally block,the exception is simply discarded.

No, that's misleading. If a finally block contains a throw or return, these cause any previously-thrown exception to be discarded. However if the finally block does not throw an exception or return, then any uncaught exception from the try block will still be thrown after the finally completes.
[ June 13, 2005: Message edited by: Jim Yingst ]
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there are differences between throwing a exception either
explicitly (or) programatically and return statement in a finally block

The return statement nullifies any previous exception and makes sure
the programs flow continues normally. Regarding a exception thrown inside
a finally block even though it discards any previous exceptions( either is try block (or) catch block) still it generates a new exception and you will see a stack trace print during runtime.
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vishnu prakash:
I think there are differences between throwing a exception either
explicitly (or) programatically and return statement in a finally block

The return statement nullifies any previous exception and makes sure
the programs flow continues normally. Regarding a exception thrown inside
a finally block even though it discards any previous exceptions( either is try block (or) catch block) still it generates a new exception and you will see a stack trace print during runtime.



just pls give final confirmation to my earlier post..
if i m putting statement "throw IOException" in finally clause it is saying this
^
ExceptionHandling.java:18: unreported exception java.io.IOException; must be cau
ght or declared to be thrown
throw new IOException();
^

what does that mean ?
and how to handle this...
How to handle the exception thrown in finally block

and as in my last post above how to catch the exception thrown in the catch block ( even it is discared by finally return statement )
what if i specifically wanted to catch the excption thrown in catch block ?
how do will do ? suppose i wanted to close some communciation I/O or etc...

pls reply
 
reply
    Bookmark Topic Watch Topic
  • New Topic