473,804 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Throw Exception and using Dispose?

This may seem like a stupid question but in C#:

Say for instance I have a set of SQL processes that I run via
ExecuteReader() . These processes return several pieces of information to the
DataReader, including any SQL exception that occurred (This may include
messages I generated b/c something didn’t check out based on programmed
requirements, but its not a runtime exception as far as SQL or C# is
concerned).

I then check this exception (or programmed error message) and perform a TROW
NEW EXCEPTION() with a more friendlier message and perform some other tasks.
I realize throw exception is costly and only do it when you have to, but its
a great way to quickly access the Application_Err or() in global asx for
logging errors and generating notifications to the programmer. The problem is
I still need to go back and dispose of the class objects that where created
before the exception is thrown. If I try to dispose of the class first then I
get an Exception of the Exception. How can manually throw exceptions and
still go back to cleanup my code when Im not using a TRY/FINALLY b/c again
these are exception I threw, not the CLR?
Of course any code I place after throw new exception() gives me an
unreachable code detected during compile.

I know Im missing something really simple here

Jun 19 '07 #1
1 1778
Why can't you instantiate your new exception and configure it with any
information you want, then dispose what you need to and then throw your
exception?
"MR E" <MR E@discussions.m icrosoft.comwrote in message
news:75******** *************** ***********@mic rosoft.com...
This may seem like a stupid question but in C#:

Say for instance I have a set of SQL processes that I run via
ExecuteReader() . These processes return several pieces of information to
the
DataReader, including any SQL exception that occurred (This may include
messages I generated b/c something didn't check out based on programmed
requirements, but its not a runtime exception as far as SQL or C# is
concerned).

I then check this exception (or programmed error message) and perform a
TROW
NEW EXCEPTION() with a more friendlier message and perform some other
tasks.
I realize throw exception is costly and only do it when you have to, but
its
a great way to quickly access the Application_Err or() in global asx for
logging errors and generating notifications to the programmer. The problem
is
I still need to go back and dispose of the class objects that where
created
before the exception is thrown. If I try to dispose of the class first
then I
get an Exception of the Exception. How can manually throw exceptions and
still go back to cleanup my code when Im not using a TRY/FINALLY b/c again
these are exception I threw, not the CLR?
Of course any code I place after throw new exception() gives me an
unreachable code detected during compile.

I know Im missing something really simple here

Jun 20 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
4871
by: gregory_may | last post by:
I have an application I created called "JpegViewer.exe". It simply loads a Jpeg file and displays in on the screen. It works great, in my lab. When I am using it at a customer site, things change. Occasionally, it blows up with an Application Exception. It seems to only die at the customer site
4
1986
by: Eric Lilja | last post by:
Hello, in my program I have a function (pseudo code): void start_mysql_service() { obtain handle start mysql service using handle if start fails close handle and throw an exception containing error description
3
6308
by: Muki Rapp | last post by:
Hi! In the example below, once the media is full, the FileSteam.WriteByte throws an exception and the code is designed to handle it. However, when the GC is invoked, it calls the Finalize of FileSteam, who is trying to flush and close the stream, but the disk is full, so the flush fails and another exception is thrown from another (GC) thread. An ugly solution would be to use GC.SuppressFinalize(fs). Is there a better solution or good...
17
1961
by: hplloyd | last post by:
Hi, I have a function that adds data to a database and I want to return true if the database was updated and false if there was a problem, so I am using a try... catch block... My problem is that I can return true ok is the routine succeeds but where do I put the return false; statement id the process fails - I get a compile error say unreachable code with whatever i try...
4
5023
by: 6tc1 | last post by:
Hi all, I have just finished debugging a windows application and have solved the problem - however, I want to be sure that I understand the problem before I move on. Before I detail the problem, this problem requires some understanding of threading concepts. Basically, the class contained both a PictureBox object as well as a corresponding Image object (they both had a copy of the same picture). The following was used to access the...
5
7513
by: Karl | last post by:
Hi, I have some code that will save the contents of a Rich Text Box in either a Text or Rich Text Format file. The code is using the SaveFileDialog and is working correctly. I have been testing the code and added in some exception handling to cater for any problems. During testing I have found that if I attempt to save to a floppy disc that is full, a System.IO.IOException is raied with the message "There is not enough space on the...
2
2598
by: pedrito | last post by:
I've got an app that downloads from several concurrent threads. Occasionally, I get an expcetion trying to read the HttpWebResponse stream... Sorry, the code isn't short, but I figured best to provide the whole method. This is run within its own thread. "Job" is a class that receives the information about the downloaded pages. The exception happens on the read where it says "EXCEPTION HERE"
6
1965
by: jason.cipriani | last post by:
Consider this program, which defines a template class who's template parameter is the type of an exception that can be thrown by members of the class: === BEGIN EXAMPLE === #include <iostream> #include <string> using namespace std;
9
1960
by: TC | last post by:
Hey All, I posted this to the Crypto users group and forgot to add the VB.Net users group. I apologize for any confusion. I have been testing a try / catch / finally block and purposely raising exceptions and I've noticed that if an exception of "Length of the data to decrypt is invalid." is raised with the CryptoStream object, later this exception will get raised a second time and thrown to the caller when trying to close the stream...
0
9593
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10343
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10088
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7633
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.