Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we use finaly block to catch exceptions without using catch block?

 
Nagaraj Shivaklara
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()
{
}


 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we cannot wrote

above is completely wrong
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic