473,395 Members | 2,436 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,395 software developers and data experts.

Using statements swallowing exceptions

ARGGGGGHH
I have a database class instantiated with a Using statement, then a
reader also instantiated with a using statement.
If the command I use to populate the reader throws an exception, then
that exception gets "swallowed" and never populates back up the callstack.
I added a threadexception handler and this catches the exception at the
top level except before my main form fully loads which does the data
loading in the load event.

Flow of events

MAIN >> Add threadexception handler that shows messagebox with exception
message >> run new form1 >> form1 load >> load data >> exception thrown
no messagebox.


After form is loaded and displayed

Combo box selectionchanged >> loads data same as form1 load >> exception
thrown and caught and messagebox displayed.

Any Ideas?

The actual code that invokes the command that throws the exception is

using(DB db = DatabaseManager.CreateDatabase())
{
... setup command
using(IDataReader Reader = db.GetReader(Command)) //db.GetReader throws
exception because of a syntax error
{
}
}

Cheers
JB
Nov 17 '05 #1
8 2059
John B <jb******@yahoo.com> wrote:
ARGGGGGHH
I have a database class instantiated with a Using statement, then a
reader also instantiated with a using statement.
If the command I use to populate the reader throws an exception, then
that exception gets "swallowed" and never populates back up the callstack.
I added a threadexception handler and this catches the exception at the
top level except before my main form fully loads which does the data
loading in the load event.


That sounds very unlikely. Using statements don't add any exception
handlers, only finally blocks. If you put a statement after your using
block, does that get executed?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Jon Skeet [C# MVP] wrote:
John B <jb******@yahoo.com> wrote:
ARGGGGGHH
I have a database class instantiated with a Using statement, then a
reader also instantiated with a using statement.
If the command I use to populate the reader throws an exception, then
that exception gets "swallowed" and never populates back up the callstack.
I added a threadexception handler and this catches the exception at the
top level except before my main form fully loads which does the data
loading in the load event.

That sounds very unlikely. Using statements don't add any exception
handlers, only finally blocks. If you put a statement after your using
block, does that get executed?

Nope. Not when I force an exception.
the Using bit is a bit misleading I think.
It would just appear that for some reason the exception is getting
swallowed when the form is in the midst of loading.
Why would appear to be the question.
My form loading is this..

Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(Appli cation_ThreadException);
Application.Run(new Form1());

in the form load...
I load a class from the db which then causes the exception in the db
layer which does not get handled or even seen.

I can force the exception and it gets caught a second time by selecting
a different item in my combo which causes a tree load from the database.

JB
Nov 17 '05 #3
John B <jb******@yahoo.com> wrote:
That sounds very unlikely. Using statements don't add any exception
handlers, only finally blocks. If you put a statement after your using
block, does that get executed?
Nope. Not when I force an exception.
the Using bit is a bit misleading I think.
It would just appear that for some reason the exception is getting
swallowed when the form is in the midst of loading.
Why would appear to be the question.
My form loading is this..

Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(Appli cation_ThreadException);
Application.Run(new Form1());

in the form load...
I load a class from the db which then causes the exception in the db
layer which does not get handled or even seen.

I can force the exception and it gets caught a second time by selecting
a different item in my combo which causes a tree load from the database.


What thread is this happening in? If it's not in the UI thread, your
ThreadExceptionEventHandler wouldn't see it - you'd need the
AppDomain.UnhandledException instead.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Hi,

I agree with Jon, this is highly unliked. The only cirscuntance where this
may happen is when the exception occur ni a worker thread. It should be
accesible from the Application.ThreadException or
AppDomian.UnhandledException

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"John B" <jb******@yahoo.com> wrote in message
news:42***********************@news.sunsite.dk...
Jon Skeet [C# MVP] wrote:
John B <jb******@yahoo.com> wrote:
ARGGGGGHH
I have a database class instantiated with a Using statement, then a
reader also instantiated with a using statement.
If the command I use to populate the reader throws an exception, then
that exception gets "swallowed" and never populates back up the
callstack.
I added a threadexception handler and this catches the exception at the
top level except before my main form fully loads which does the data
loading in the load event.

That sounds very unlikely. Using statements don't add any exception
handlers, only finally blocks. If you put a statement after your using
block, does that get executed?

Nope. Not when I force an exception.
the Using bit is a bit misleading I think.
It would just appear that for some reason the exception is getting
swallowed when the form is in the midst of loading.
Why would appear to be the question.
My form loading is this..

Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(Appli cation_ThreadException);
Application.Run(new Form1());

in the form load...
I load a class from the db which then causes the exception in the db layer
which does not get handled or even seen.

I can force the exception and it gets caught a second time by selecting a
different item in my combo which causes a tree load from the database.

JB

Nov 17 '05 #5
Jon Skeet [C# MVP] wrote:
John B <jb******@yahoo.com> wrote:
That sounds very unlikely. Using statements don't add any exception
handlers, only finally blocks. If you put a statement after your using
block, does that get executed?


Nope. Not when I force an exception.
the Using bit is a bit misleading I think.
It would just appear that for some reason the exception is getting
swallowed when the form is in the midst of loading.
Why would appear to be the question.
My form loading is this..

Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(App lication_ThreadException);
Application.Run(new Form1());

in the form load...
I load a class from the db which then causes the exception in the db
layer which does not get handled or even seen.

I can force the exception and it gets caught a second time by selecting
a different item in my combo which causes a tree load from the database.

What thread is this happening in? If it's not in the UI thread, your
ThreadExceptionEventHandler wouldn't see it - you'd need the
AppDomain.UnhandledException instead.

Nope, UI thread. Or rather no threading is involved, so unless
Application.Run runs on a different thread and then instantiates the new
form1 on that thread and then somehow magically switches it, it is the
one (and only) thread.
I havent tried AppDomain.UnhandledException though, so might try that
even though it seems unlikely that it would pick it up as it does not
show up in the JIT debugger as "normal" unhandled exceptions do.

JB
Nov 17 '05 #6
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

I agree with Jon, this is highly unliked. The only cirscuntance where this
may happen is when the exception occur ni a worker thread. It should be
accesible from the Application.ThreadException or
AppDomian.UnhandledException

Cheers,

See my reply to Jon. :)
JB
Nov 17 '05 #7
John B <jb******@yahoo.com> wrote:
Nope, UI thread. Or rather no threading is involved, so unless
Application.Run runs on a different thread and then instantiates the new
form1 on that thread and then somehow magically switches it, it is the
one (and only) thread.


In that case, could you post a short but complete program which
demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

(In passing, it's not really a good idea to do database operations on
the UI thread - it could take a long time to open a connection, or fail
to open it, for instance.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
Jon Skeet [C# MVP] wrote:
John B <jb******@yahoo.com> wrote:
Nope, UI thread. Or rather no threading is involved, so unless
Application.Run runs on a different thread and then instantiates the new
form1 on that thread and then somehow magically switches it, it is the
one (and only) thread.

In that case, could you post a short but complete program which
demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Yeah, will have to try and do one because it is mucho wierdo.
(In passing, it's not really a good idea to do database operations on
the UI thread - it could take a long time to open a connection, or fail
to open it, for instance.)


Agreed. ;)

JB
Nov 17 '05 #9

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

Similar topics

1
by: JT | last post by:
We have an exception handler which needs some tweaking because when we changed compilers, the embedded assembly didn't work. Ideally, we would like a complete C++ solution, without assembly...
1
by: gemel | last post by:
I wish to implement error recovery using the Exception mechanism However, I'm not sure I fully appreciate some of the detail in which this works. The code that contains a potential error is...
12
by: bj7lewis | last post by:
I am working on a project I want to add a few files as resource to access(copy them to FS and use) at runtime. So far in VS.NET IDE, I Add Files to the project and set its Build Action to...
40
by: Mark P | last post by:
I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
1
by: jmalone | last post by:
I have a python script that I need to freeze on AIX 5.1 (customer has AIX and does not want to install Python). The python script is pretty simple (the only things it imports are sys and socket)....
11
by: O.B. | last post by:
Does C# support anything like PHP's break command that optionally accepts a parameter specifying how many loops to break out of?
65
by: Spiros Bousbouras | last post by:
Has anyone found that declaring variables register affected speed of execution ? If yes on what hardware and around which year ?
5
by: cronusf | last post by:
Consider the following code: using(StreamReader fin = new StreamReader(filename)) { // do work with fin return ReferenceToSomeObjectBasedOnLoadedData(); } Since using expands to a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.