| Author |
Can we use finaly block to catch exceptions without using catch block?
|
Nagaraj Shivaklara
Ranch Hand
Joined: Dec 16, 2008
Posts: 72
|
|
Hi All,
I have two questions:
1) Can we use finally block to catch exceptions without using catch block after the try block?
2) Can we write finally block before catch block? like below :
In the below case where should we catch exceptions? (in finally block or catch block)
try
{
}
finally{
}
catch()
{
}
|
Thanks n Regards,
Nagaraj S K
|
 |
Prasad Kharkar
Ranch Hand
Joined: Mar 07, 2010
Posts: 438
|
|
We cannot use finally block to catch exceptions
what the finally block means that whatever we write in the finally block, it is executed regardless the exception is thrown or not
|
SCJP 6 [86%] June 30th, 2010
If you find any post useful, click the "plus one" sign on the right
|
 |
Prasad Kharkar
Ranch Hand
Joined: Mar 07, 2010
Posts: 438
|
|
we cannot wrote
above is completely wrong
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Invalid Idiom!
<edit>
as a side note : for better practice, even finally block should not throw an exception, i.e, finally block should not complete execution abruptly!
</edit>
|
 |
Nagaraj Shivaklara
Ranch Hand
Joined: Dec 16, 2008
Posts: 72
|
|
Hi Prasad,
I agree with you, but we can write finally after the try block without catch block is still valid right?
In the above case what if try block thows some exceptions!!! where do we need to catch them?
I hope you got my point.
Thanks in Advance
Nagaraj SK
Prasad Kharkar wrote:We cannot use finally block to catch exceptions
what the finally block means that whatever we write in the finally block, it is executed regardless the exception is thrown or not
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Nagaraj Shivaklara wrote:In the above case what if try block thows some exceptions!!! where do we need to catch them?
<edit>Prasad mentioned that earlier </edit>
Prasad Kharkar wrote:
what the finally block means that whatever we write in the finally block, it is executed regardless the exception is thrown or not
|
 |
Prasad Kharkar
Ranch Hand
Joined: Mar 07, 2010
Posts: 438
|
|
yes
we can write finally block even if we are not writing the catch block
but this does not mean that we are catching the exception in the try block (I think this is where you are mistaken)
the method in which we are writing this try block and directly finally block does not handle the exception
if we do not handle the exception , then we need to declare that the method throws exception but this is not required for the RuntimeException
see the code for more clarification
hope this helps now
|
 |
Nagaraj Shivaklara
Ranch Hand
Joined: Dec 16, 2008
Posts: 72
|
|
Thanks a lot Prasad, now i understood.
Prasad Kharkar wrote:yes
we can write finally block even if we are not writing the catch block
but this does not mean that we are catching the exception in the try block (I think this is where you are mistaken)
the method in which we are writing this try block and directly finally block does not handle the exception
if we do not handle the exception , then we need to declare that the method throws exception but this is not required for the RuntimeException
see the code for more clarification
hope this helps now
|
 |
 |
|
|
subject: Can we use finaly block to catch exceptions without using catch block?
|
|
|