Catch All Exceptions for Console Based Program

S

Simon Johnson

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 can tell, is it possible to do the
same with a console application?

Simon.
 
M

Morten Wennevik

put all code in Main() inside a try/catch(/finally) block.
Main is where your program starts and any exception not caught elsewhere
in your code will end up in main before your program crashes.
 
N

Nicholas Paldino [.NET/C# MVP]

Morten,

That's not completely true. You can have multiple threads in a console
application, and if an exception is thrown on one of those threads, then you
will not catch it by wrapping the code in a try/catch block. Take for
example the following code:

static void Main(string[] args)
{
try
{
// Create a thread.
Thread pobjThread = new Thread(new ThreadStart(DoSomething));

// Start the thread.
pobjThread.Start();

// Wait five seconds.
Thread.Sleep(5000);
}
catch (Exception e)
{
Console.WriteLine("Exception on Main thread:")
Console.WriteLine(e);
}

// Get out.
return;
}

private static void DoSomething()
{
// Throw an exception.
throw new Exception();
}

This will crash and not be caught by the catch block.

Rather, you need to attach to the UnhandledException event on the
current AppDomain, like this:

static void Main(string[] args)
{
// Wire up an event handler for unhandled exceptions.
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(UnhandledException);

try
{
// Create a thread.
Thread pobjThread = new Thread(new ThreadStart(DoSomething));

// Start the thread.
pobjThread.Start();

// Wait five seconds.
Thread.Sleep(5000);
}
catch (Exception e)
{
Console.WriteLine(e);
}

// Get out.
return;
}

private static void UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
// Write the exception.
Console.WriteLine("AppDomain Unhandled Exception Event Handler:");
Console.WriteLine(e.ExceptionObject);
}

private static void DoSomething()
{
// Throw an exception.
throw new Exception();
}
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

put all code in Main() inside a try/catch(/finally) block.
Main is where your program starts and any exception not caught elsewhere
in your code will end up in main before your program crashes.
 
J

Jackson Davis [MSFT]

-----Original Message-----
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 can tell, is it possible to do the
same with a console application?

Simon.


.

Vectored Exception Handling is a new exception mechanism
introduced in windows XP that allows you to catch
exceptions in a "Thread Independent" manner. See:

http://msdn.microsoft.com/msdnmag/issues/01/09/hood/default
..aspx
for more information. You can also add your own "backstop"
to your application with by calling
SetUnhandledExceptionFilter on your process which will
allow you place a handler at the bottom of the exception
stack to execute when any exception propagates out far
enough. See:
http://msdn.microsoft.com/library/default.asp?
url=/library/en-
us/debug/base/setunhandledexceptionfilter.asp for more
information.

Jackson Davis [MSFT]
--

This posting is provided "AS IS" with no warranties, and
confers no rights.
Use of included script samples are subject to the terms
specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all
responses to this
message are best directed to the newsgroup/thread from
which they
originated.
 
E

Eric Newton

Or the simple way could be to use the AppDomain.UnhandledException event?
;-)

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
(e-mail address removed)-software.com [remove the first "CC."]

Jackson Davis said:
-----Original Message-----
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 can tell, is it possible to do the
same with a console application?

Simon.


.

Vectored Exception Handling is a new exception mechanism
introduced in windows XP that allows you to catch
exceptions in a "Thread Independent" manner. See:

http://msdn.microsoft.com/msdnmag/issues/01/09/hood/default
.aspx
for more information. You can also add your own "backstop"
to your application with by calling
SetUnhandledExceptionFilter on your process which will
allow you place a handler at the bottom of the exception
stack to execute when any exception propagates out far
enough. See:
http://msdn.microsoft.com/library/default.asp?
url=/library/en-
us/debug/base/setunhandledexceptionfilter.asp for more
information.

Jackson Davis [MSFT]
--

This posting is provided "AS IS" with no warranties, and
confers no rights.
Use of included script samples are subject to the terms
specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all
responses to this
message are best directed to the newsgroup/thread from
which they
originated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top