473,397 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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 10078
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

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

Similar topics

4
by: Hasani | last post by:
http://www.skidmore.edu/~h_blackw/finally.gif I can only catch the exception thrown in the finally code section. even if I do catch(InvalidOperationException)
8
by: Z D | last post by:
Hi, I was wondering what's the point of "finally" is in a try..catch..finally block? Isn't it the same to put the code that would be in the "finally" section right after the try/catch block?...
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
18
by: Simon | last post by:
I was of the impression that code placed after a Try...Catch block was only executed if there was no exception thrown. I've got some VB.net code as part of a Windows form that executes even...
20
by: Woody Splawn | last post by:
In a message yesterday titled Try Catch Question I got numerous responses. Thank you to all. After all the smoke clears I guess the question is where to place the Return True statement in the...
7
by: Sean Kirkpatrick | last post by:
I got caught with my pants down the other day when trying to explain Try...Catch...Finally and things didn't work as I had assumed. Perhaps someone can explain to me the purpose of Finally. I've...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
5
by: Morten Snedker | last post by:
The use of Try in the Finally part - is that overkill?. I think of the code failing before opening sqlCon - that would generate an error in the Finally part. How would Finally handle that? Try...
4
by: cj | last post by:
my old code Try Dim sw As New System.io.StreamWriter(fileName, True) sw.WriteLine(strToWrite) sw.Close() Catch End Try my new code
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.