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

Console application thread exception handling

Nak
Hi there,

I have a very simple console application that I'm trying to handle the
thread exception on, as I understand it there is no Application object
available to a console application, so I can't use this. Instead you need
to use "Thread.GetAppDomain.UnhandledException", this "works" to a certain
extent but from the look if it you merely get added to a chain of event
handlers, and the runtime is still above you, so I can't use this because
the "unhandled exception" dialog appears *before* my event is even fired.

Does anyone have any other ways of achieving this? I need to be able to
catch *all* unhandled exceptions in a console application within the same
event preferably. Cheers in advance.

Nick.
Nov 21 '05 #1
5 6966
Nak
Not to worry, I've resolved the issue by creating a new threading and
joining it. Now any exception that occurs in this thread is handled by me
first and not the system.

Nick.

"Nak" <a@a.com> wrote in message
news:eq**************@TK2MSFTNGP15.phx.gbl...
Hi there,

I have a very simple console application that I'm trying to handle the
thread exception on, as I understand it there is no Application object
available to a console application, so I can't use this. Instead you need
to use "Thread.GetAppDomain.UnhandledException", this "works" to a certain
extent but from the look if it you merely get added to a chain of event
handlers, and the runtime is still above you, so I can't use this because
the "unhandled exception" dialog appears *before* my event is even fired.

Does anyone have any other ways of achieving this? I need to be able
to catch *all* unhandled exceptions in a console application within the
same event preferably. Cheers in advance.

Nick.

Nov 21 '05 #2
Nak
Not to worry, I've resolved the issue by creating a new threading and
joining it. Now any exception that occurs in this thread is handled by me
first and not the system.

Nick.

"Nak" <a@a.com> wrote in message
news:eq**************@TK2MSFTNGP15.phx.gbl...
Hi there,

I have a very simple console application that I'm trying to handle the
thread exception on, as I understand it there is no Application object
available to a console application, so I can't use this. Instead you need
to use "Thread.GetAppDomain.UnhandledException", this "works" to a certain
extent but from the look if it you merely get added to a chain of event
handlers, and the runtime is still above you, so I can't use this because
the "unhandled exception" dialog appears *before* my event is even fired.

Does anyone have any other ways of achieving this? I need to be able
to catch *all* unhandled exceptions in a console application within the
same event preferably. Cheers in advance.

Nick.

Nov 21 '05 #3
Nick,
You are correct, using Addhandler on AppDomain.UnhandledException adds
yourself to the end of chain of handlers, normally you will be the only
handler.

Remember http://msdn.microsoft.com/msdnmag/is...T/default.aspx
states that "unhandled exceptions that occur on the application's main
thread cause the application to terminate".

Ergo the Main routine is special, it effectively has an extra "handler",
actually I suspect Main itself is in a Try/Catch block, & its this Try/Catch
that you are seeing.

Normally I use AppDomain.UnhandledException in my Console applications, plus
I have a Try/Catch wrapping any logic within Main, especially if its of any
consequence.

Depending on what this Console application is doing, creating a new thread
or putting a Try/Catch in Main either or both may be needed.

Unfortunately I don't know of an easy way to call the list of handlers on
AppDomain.UnhandledException like Application.OnThreadException provides for
Application.ThreadException's list of handlers... I normally just have the
Try/Catch in Main & AppDomain.UnhandledException to both call the same
common routine. Alternatively the Try/Catch in Main can simply call your
handler on AppDomain.UnhandledException, especially if you know you will
only have one.

Hope this helps
Jay


"Nak" <a@a.com> wrote in message
news:eq**************@TK2MSFTNGP15.phx.gbl...
Hi there,

I have a very simple console application that I'm trying to handle the
thread exception on, as I understand it there is no Application object
available to a console application, so I can't use this. Instead you need
to use "Thread.GetAppDomain.UnhandledException", this "works" to a certain
extent but from the look if it you merely get added to a chain of event
handlers, and the runtime is still above you, so I can't use this because
the "unhandled exception" dialog appears *before* my event is even fired.

Does anyone have any other ways of achieving this? I need to be able
to catch *all* unhandled exceptions in a console application within the
same event preferably. Cheers in advance.

Nick.

Nov 21 '05 #4
Nak
Hi Jay,
You are correct, using Addhandler on AppDomain.UnhandledException adds
yourself to the end of chain of handlers, normally you will be the only
handler.

Remember http://msdn.microsoft.com/msdnmag/is...T/default.aspx
states that "unhandled exceptions that occur on the application's main
thread cause the application to terminate".
Aah, that would make sence why creating a new thread solved my issue.
Ergo the Main routine is special, it effectively has an extra "handler",
actually I suspect Main itself is in a Try/Catch block, & its this
Try/Catch that you are seeing.

Normally I use AppDomain.UnhandledException in my Console applications,
plus I have a Try/Catch wrapping any logic within Main, especially if its
of any consequence.
I've taken the same approach as yourself on a few occasions, only
handling the application entry point for "unhandled exceptions". It's
worked well but I thought I'd try a different angle.
Depending on what this Console application is doing, creating a new thread
or putting a Try/Catch in Main either or both may be needed.
Yup, just what I ended doing.
Unfortunately I don't know of an easy way to call the list of handlers on
AppDomain.UnhandledException like Application.OnThreadException provides
for Application.ThreadException's list of handlers... I normally just have
the Try/Catch in Main & AppDomain.UnhandledException to both call the same
common routine. Alternatively the Try/Catch in Main can simply call your
handler on AppDomain.UnhandledException, especially if you know you will
only have one.


It would be nice to be able to clear event handlers but then again I
should imagine the framework would become a whole lot insecure wouldnt it?

Cheers for the help, much appreciated, sorry I haven't got time to type
more! Cheers...

Nick.
Nov 21 '05 #5
Nick,
It would be nice to be able to clear event handlers but then again I
should imagine the framework would become a whole lot insecure wouldnt it? As you know, you can use RemoveHandler to remove one of your handlers from
any event.

If its your event, you can change the underlying list of event handlers. If
your class has "MyEvent" there will be a hidden "MyEventEvent" field that is
the underlying delegate. Setting this field to Nothing effectively clear the
event handlers...

VS.NET 2005 (aka Whidbey, due out later in 2005) will allow "custom events",
where you can have control over what delegate the events are stored in:

http://msdn2.microsoft.com/library/6hwhs172.aspx
http://msdn2.microsoft.com/library/yt1k2w4e.aspx
http://msdn2.microsoft.com/library/wf33s4w7.aspx

With "custom events" clearing the list should be easier, at least not rely
on an undocumented feature ;-)

Now whether you should clear the list of handlers or not is a matter of
another discussion :-)

Hope this helps
Jay

"Nak" <a@a.com> wrote in message
news:uM**************@TK2MSFTNGP10.phx.gbl... Hi Jay,
You are correct, using Addhandler on AppDomain.UnhandledException adds
yourself to the end of chain of handlers, normally you will be the only
handler.

Remember http://msdn.microsoft.com/msdnmag/is...T/default.aspx
states that "unhandled exceptions that occur on the application's main
thread cause the application to terminate".


Aah, that would make sence why creating a new thread solved my issue.
Ergo the Main routine is special, it effectively has an extra "handler",
actually I suspect Main itself is in a Try/Catch block, & its this
Try/Catch that you are seeing.

Normally I use AppDomain.UnhandledException in my Console applications,
plus I have a Try/Catch wrapping any logic within Main, especially if its
of any consequence.


I've taken the same approach as yourself on a few occasions, only
handling the application entry point for "unhandled exceptions". It's
worked well but I thought I'd try a different angle.
Depending on what this Console application is doing, creating a new
thread or putting a Try/Catch in Main either or both may be needed.


Yup, just what I ended doing.
Unfortunately I don't know of an easy way to call the list of handlers on
AppDomain.UnhandledException like Application.OnThreadException provides
for Application.ThreadException's list of handlers... I normally just
have the Try/Catch in Main & AppDomain.UnhandledException to both call
the same common routine. Alternatively the Try/Catch in Main can simply
call your handler on AppDomain.UnhandledException, especially if you know
you will only have one.


It would be nice to be able to clear event handlers but then again I
should imagine the framework would become a whole lot insecure wouldnt it?

Cheers for the help, much appreciated, sorry I haven't got time to type
more! Cheers...

Nick.

Nov 21 '05 #6

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

Similar topics

2
by: Michael Zhang | last post by:
My project uses Python-2.3.4 + Tkinter + PIL-1.1.4 to retrieve images from server and display those images. I created a thread (also a separate toplevel window) for displaying images and another...
2
by: Gulshan Oshan | last post by:
I want to implement a simple console that continuously listens for an event from a custom object. I am unable to capture the events from the object. If I subscribe to the events in a windows app...
5
by: Simon Johnson | last post by:
Recently, a thread appeared which asked how to create a "catch all" method for when an exception occurs within a program. The solution given only works for Windows Forms applications, as far as i...
17
by: MumboJumbo | last post by:
Hi I have a really basic question hopefully some can help me with: Can you write a (i.e. one) C# project that works from the cmd line and gui? I seems if i write a GUI app it can't write to...
3
by: Dean Slindee | last post by:
I have a exception handling class that could be called from either a windows project app or a console project app. Is there any way for this class to determine which type of app called it without...
0
by: Nak | last post by:
Hi there, I have a very simple console application that I'm trying to handle the thread exception on, as I understand it there is no Application object available to a console application, so I...
1
by: John Wright | last post by:
I am running a console application that connects to an Access database (8 million rows) and converts it to a text file and then cleans and compacts the database. When it runs I get the following...
6
by: Mantorok | last post by:
Hi all I have a console application that runs every night via a scheduled task, there is a try-catch block in my Main class, whenever an error is caught it is written to a log file and then...
4
by: =?Utf-8?B?QWxleGFuZGVy?= | last post by:
Hi! I am new to C#. I read two C# books in the last two days which only covered the basics. And now I am on my third day. The books were really bad, because they did not cover windows applications,...
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: 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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...

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.