469,275 Members | 1,817 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

How to catch exception in Finally Block

HI,

i want to know if there is any exception in finally then how to catch exception in Finally Block.
Jul 24 '07 #1
7 9869
TRScheel
638 Expert 512MB
HI,

i want to know if there is any exception in finally then how to catch exception in Finally Block.
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.    // Code that sometimes throws exception
  4. }
  5. catch
  6. {
  7. }
  8. finally
  9. {
  10.     try
  11.     {
  12.          // Another snippet of code that might throw an exception
  13.     }
  14.     catch { }
  15. }
  16.  
In short, you just need to embed try / catch blocks within the finally block.
Jul 24 '07 #2
nmsreddi
366 256MB
Hello

I dint get why you need to catch exception in finally block

Finally is used to execute some code every time even exception is not raised probably (connection closing ) will be written in finally ,if any exception raises then it automatically goes to catch block
Jul 25 '07 #3
Hello

I dint get why you need to catch exception in finally block

Finally is used to execute some code every time even exception is not raised probably (connection closing ) will be written in finally ,if any exception raises then it automatically goes to catch block

hi,
Thank you for reply.i just want to know what happend if any exception raised in finally block.
Jul 25 '07 #4
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.    // Code that sometimes throws exception
  4. }
  5. catch
  6. {
  7. }
  8. finally
  9. {
  10.     try
  11.     {
  12.          // Another snippet of code that might throw an exception
  13.     }
  14.     catch { }
  15. }
  16.  
In short, you just need to embed try / catch blocks within the finally block.

hi,

Thankyou for reply.

there is any other way to catch exceprtion in finally block rather then try catch?
Jul 25 '07 #5
RoninZA
78
There is only ONE way to catch exceptions in C# and that's with try...catch
Jul 25 '07 #6
TRScheel
638 Expert 512MB
There is only ONE way to catch exceptions in C# and that's with try...catch
A using block should do it too, but for some reason there's a bug that lets it persist the exceptions. If you decompile how using is suppossed to work, it basically says:

Expand|Select|Wrap|Line Numbers
  1. object MyObject = new object();
  2. try
  3. {
  4.     MyObject.MyFunction();
  5. }
  6. catch { }
  7. MyObject.Dispose();
should be the same as:

Expand|Select|Wrap|Line Numbers
  1. using(object MyObject = new object())
  2. {
  3.    MyObject.MyFunction();
  4. }
Jul 25 '07 #7
Then why didn't we write every code in finally instead of writing anywhere else
Mar 25 '11 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Pohihihi | last post: by
23 posts views Thread by VB Programmer | last post: by
18 posts views Thread by Simon | last post: by
20 posts views Thread by Woody Splawn | last post: by
7 posts views Thread by Sean Kirkpatrick | last post: by
32 posts views Thread by cj | last post: by
5 posts views Thread by Morten Snedker | last post: by
4 posts views Thread by cj | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.