• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()
{
}


 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we cannot wrote

above is completely wrong
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
reply
    Bookmark Topic Watch Topic
  • New Topic