[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Unhandled Exception in Console Application

AG

4/8/2008 8:35:00 PM

Can anyone point me to an example or info on handling unhandled exceptions
in a console application?
Preferrably VB.

Thanks,

--

AG
Email: discussATadhdataDOTcom


10 Answers

Peter Duniho

4/8/2008 9:44:00 PM

0

On Tue, 08 Apr 2008 13:34:30 -0700, AG <NOSPAMagiamb@newsgroup.nospam>
wrote:

> Can anyone point me to an example or info on handling unhandled
> exceptions
> in a console application?
> Preferrably VB.

The MSDN documentation for the AppDomain.UnhandledException event has lots
of information and a code sample:
http://msdn2.microsoft.com/en-us/library/system.appdomain.unhandledexce...

AG

4/9/2008 2:38:00 AM

0

Thanks for the quick reply.
That looks good. I will try it.

--

AG
Email: discussATadhdataDOTcom
"Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com> wrote in message
news:op.t9banlie8jd0ej@petes-computer.local...
> On Tue, 08 Apr 2008 13:34:30 -0700, AG <NOSPAMagiamb@newsgroup.nospam>
> wrote:
>
>> Can anyone point me to an example or info on handling unhandled
>> exceptions
>> in a console application?
>> Preferrably VB.
>
> The MSDN documentation for the AppDomain.UnhandledException event has lots
> of information and a code sample:
> http://msdn2.microsoft.com/en-us/library/system.appdomain.unhandledexce...


AG

4/9/2008 1:18:00 PM

0

That enables me to log the exception, but how can I prevent the Windows
'Myapp has encountered a problem and needs to close...' dialog from popping
up?

--

AG
Email: discussATadhdataDOTcom
"Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com> wrote in message
news:op.t9banlie8jd0ej@petes-computer.local...
> On Tue, 08 Apr 2008 13:34:30 -0700, AG <NOSPAMagiamb@newsgroup.nospam>
> wrote:
>
>> Can anyone point me to an example or info on handling unhandled
>> exceptions
>> in a console application?
>> Preferrably VB.
>
> The MSDN documentation for the AppDomain.UnhandledException event has lots
> of information and a code sample:
> http://msdn2.microsoft.com/en-us/library/system.appdomain.unhandledexce...


Luc E. Mistiaen

4/9/2008 3:04:00 PM

0

you can try
if (Args.IsTerminating) Environment.Exit (0) ;

where Args is your UnhandledExceptionEventArgs

/LM

"AG" <NOSPAMagiamb@newsgroup.nospam> wrote in message
news:%23GQmlQkmIHA.5268@TK2MSFTNGP05.phx.gbl...
> That enables me to log the exception, but how can I prevent the Windows
> 'Myapp has encountered a problem and needs to close...' dialog from
> popping up?
>
> --
>
> AG
> Email: discussATadhdataDOTcom
> "Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com> wrote in message
> news:op.t9banlie8jd0ej@petes-computer.local...
>> On Tue, 08 Apr 2008 13:34:30 -0700, AG <NOSPAMagiamb@newsgroup.nospam>
>> wrote:
>>
>>> Can anyone point me to an example or info on handling unhandled
>>> exceptions
>>> in a console application?
>>> Preferrably VB.
>>
>> The MSDN documentation for the AppDomain.UnhandledException event has
>> lots of information and a code sample:
>> http://msdn2.microsoft.com/en-us/library/system.appdomain.unhandledexce...
>
>


Peter Duniho

4/9/2008 4:22:00 PM

0

On Wed, 09 Apr 2008 06:18:04 -0700, AG <NOSPAMagiamb@newsgroup.nospam>
wrote:

> That enables me to log the exception, but how can I prevent the Windows
> 'Myapp has encountered a problem and needs to close...' dialog from
> popping up?

You can try the solution Luc suggested. I'd say there's at least a 50/50
chance it'll work (I'd test it myself, but I figure you can do that just
as easily, and at the moment I am doing other things :) ).

If it doesn't, then you may not be able to prevent the dialog from
appearing.

Of course, this assumes the exception is happening somewhere that you are
unable to provide your own try/catch block in order to handle the
exception. Your original question implies this, but if it's not really
true, then obviously the best solution is to actually handle the
exception. :)

Pete

AG

4/9/2008 7:49:00 PM

0

Thanks both.
That works.
I haven't done a console app before and It is a very small one. I could
easily have wrapped that whole thing in a try/catch, but this way I learned
much more :).

--

AG
Email: discussATadhdataDOTcom
"Luc E. Mistiaen" <luc.mistiaen@advalvas.be.no.spam> wrote in message
news:e6$YxLlmIHA.5260@TK2MSFTNGP03.phx.gbl...
> you can try
> if (Args.IsTerminating) Environment.Exit (0) ;
>
> where Args is your UnhandledExceptionEventArgs
>
> /LM
>
> "AG" <NOSPAMagiamb@newsgroup.nospam> wrote in message
> news:%23GQmlQkmIHA.5268@TK2MSFTNGP05.phx.gbl...
>> That enables me to log the exception, but how can I prevent the Windows
>> 'Myapp has encountered a problem and needs to close...' dialog from
>> popping up?
>>
>> --
>>
>> AG
>> Email: discussATadhdataDOTcom
>> "Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com> wrote in message
>> news:op.t9banlie8jd0ej@petes-computer.local...
>>> On Tue, 08 Apr 2008 13:34:30 -0700, AG <NOSPAMagiamb@newsgroup.nospam>
>>> wrote:
>>>
>>>> Can anyone point me to an example or info on handling unhandled
>>>> exceptions
>>>> in a console application?
>>>> Preferrably VB.
>>>
>>> The MSDN documentation for the AppDomain.UnhandledException event has
>>> lots of information and a code sample:
>>> http://msdn2.microsoft.com/en-us/library/system.appdomain.unhandledexce...
>>
>>
>
>


Jeroen Mostert

4/9/2008 8:02:00 PM

0

Luc E. Mistiaen wrote:
> you can try
> if (Args.IsTerminating) Environment.Exit (0) ;
>
> where Args is your UnhandledExceptionEventArgs
>
Be aware that this technique does not scale to services or multiple
appdomains, and it will also prevent you from diagnosing the unhandled
exception under a debugger (you can set a breakpoint on the event callback,
but that isn't convenient at all). Finally, the usual step of writing an
error to the event log will be skipped.

I also suggest passing a non-zero value to Environment.Exit(), since
processes that check the exit status (if any) will usually interpret a zero
exit status as success, which is a tad misleading.

In short, I'm not at all happy that this actually works. :-)

--
J.

AG

4/10/2008 1:59:00 AM

0

Thanks for the info.
In this case it will be fine as the app is just for my own use.
However, I am interested in how you think it should be handled.

--

AG
Email: discussATadhdataDOTcom
"Jeroen Mostert" <jmostert@xs4all.nl> wrote in message
news:47fd20a7$0$14342$e4fe514c@news.xs4all.nl...
> Luc E. Mistiaen wrote:
>> you can try
>> if (Args.IsTerminating) Environment.Exit (0) ;
>>
>> where Args is your UnhandledExceptionEventArgs
>>
> Be aware that this technique does not scale to services or multiple
> appdomains, and it will also prevent you from diagnosing the unhandled
> exception under a debugger (you can set a breakpoint on the event
> callback, but that isn't convenient at all). Finally, the usual step of
> writing an error to the event log will be skipped.
>
> I also suggest passing a non-zero value to Environment.Exit(), since
> processes that check the exit status (if any) will usually interpret a
> zero exit status as success, which is a tad misleading.
>
> In short, I'm not at all happy that this actually works. :-)
>
> --
> J.


Luc E. Mistiaen

4/10/2008 2:18:00 PM

0

> Finally, the usual step of writing an error to the event log will be
> skipped.

I suppose it was precisely waht the OP intended to do: his own logging. You
just do it before exiting.

>
> I also suggest passing a non-zero value to Environment.Exit(), since
> processes that check the exit status (if any) will usually interpret a
> zero exit status as success, which is a tad misleading.

Of course, it was just an example.

>
> In short, I'm not at all happy that this actually works. :-)

Neither me, but its the only way I found to suppress the unwanted pop-up. I
would have much preferred some boolean to be set in the argument object to
tell the run-time I handled to event to my taste (knowing that the program
would terminate anyway).

/LM


Luc E. Mistiaen

4/10/2008 2:20:00 PM

0

> Of course, this assumes the exception is happening somewhere that you are
> unable to provide your own try/catch block in order to handle the
> exception. Your original question implies this, but if it's not really
> true, then obviously the best solution is to actually handle the
> exception. :)

Yes, this really should be used as a last chance to log a catastrophic
unrecoverable error...

/LM