uncaught exception handlers
-
Correct me if i'm wrong, but generally catching things using catch (Exception err) is bad practice? You should try any catch specific exceptions otherwise you get a much greater performance hit I believe. *A point always worth mentioning (particularily to people new to the language); try to avoid using try...catch as much as possible due to the massive performance hit it (hundreds of times slower to process try...catch than simple math functions).
-
For a console app, you could put try/catch around the guts of your main() function.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001Indeed, I always put a try/catch in the Main.
-
Thanks for the link, an interesting article. I probably didn't specify 'massive' which didn't help; I meant relative to performing an if test to prevent the exception.
-
Thanks N a v a n e e t h, What do you mean "windows applications"? I have checked again in VS 2008, there is not a project type names "windows applications". Do you mean Windows Forms application? regards, George
George_George wrote:
Windows Forms application?
Yes. Also you can wrap the
main()
method intry catch
blocks. So it can handle all exceptions.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
George_George wrote:
Windows Forms application?
Yes. Also you can wrap the
main()
method intry catch
blocks. So it can handle all exceptions.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
No, N a v a n e e t h. Not catch all exceptions, you can not catch exception from other threads? Right? regards, George
-
Thanks PIEBALDconsult, Good reference. :-) regards, George
-
Thanks for the link, an interesting article. I probably didn't specify 'massive' which didn't help; I meant relative to performing an if test to prevent the exception.
Thanks Derek, What do you mean "didn't specify 'massive' which didn't help" and "relative to performing an if test to prevent the exception"? Could you show more description or some pseudo code about your approach please? regards, George
-
No, N a v a n e e t h. Not catch all exceptions, you can not catch exception from other threads? Right? regards, George
George_George wrote:
you can not catch exception from other threads? Right?
Yes. You are right. I missed that. Hook
AppDomain.UnhandledException
which handles all exception other than the ones I specified in my first message.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
George_George wrote:
you can not catch exception from other threads? Right?
Yes. You are right. I missed that. Hook
AppDomain.UnhandledException
which handles all exception other than the ones I specified in my first message.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
You mean "Also exceptions occurring in unmanaged resources won't be handled too" will not be handled? regards, George
-
Spacix One wrote:
which can allow you diagnose it
And if nothing else, I guess that's the answer to the orriginal question; set a break point in the catch block and Visual Studio will tell you the type of the exception (and hence you can modify the code to catch exactly that). Does anyone know if a try...catch block affects the performance of the try block code? I know it has a performance hit on hitting the block, but I wonder if it has a continuing effect beyond that.
Derek Bartram wrote:
Does anyone know if a try...catch block affects the performance of the try block code? I know it has a performance hit on hitting the block, but I wonder if it has a continuing effect beyond that.
I once worked on a project where the Project Lead insisted on try catch blocks in every bloody method. I did some performance testing and could determine definitive costs with the setup and teardown of the block, but was unable to measure any discernable difference to the code internally. However the code was entirely managed, perhaps wrapping unmanaged code has other implications.
I'm largely language agnostic
After a while they all bug me :doh:
-
Hi John, I think in this approach, we can only catch exception from main thread, right? (Suppose we create new threads from main methods, then we can not catch exception from other threads in main method?) regards, George
If you wanted to go that far you can catch errors in your thread method also... Are you looking for on error resume next type functionally? If so might be better to use VB.NET as C# doesn't allow for that coding horror :) You need to remember the following: Using exceptions to control programming logic is is the same as having car insurance to repair the damange after rolling off a cliff instead of setting the parking break to prevent it from rolling away.
-Spacix All your skynet questions[^] belong to solved
-
You mean "Also exceptions occurring in unmanaged resources won't be handled too" will not be handled? regards, George
George_George wrote:
"Also exceptions occurring in unmanaged resources won't be handled too"
Yes. It won't be handled. Because it runs out of CLR.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
If you wanted to go that far you can catch errors in your thread method also... Are you looking for on error resume next type functionally? If so might be better to use VB.NET as C# doesn't allow for that coding horror :) You need to remember the following: Using exceptions to control programming logic is is the same as having car insurance to repair the damange after rolling off a cliff instead of setting the parking break to prevent it from rolling away.
-Spacix All your skynet questions[^] belong to solved
Thanks Spacix, 1.
Spacix One wrote:
If you wanted to go that far you can catch errors in your thread method also...
So, no means to catch exception from another thread, right? :-) 2.
Spacix One wrote:
on error resume next type functionally? If so might be better to use VB.NET as C# doesn't allow for that coding horror
Sorry, I have no experience in VB. Could you say something alternative to describe your ideas please? I do not quite understand, especially what do you mean "on error resume next type functionally". :-) regards, George
-
George_George wrote:
"Also exceptions occurring in unmanaged resources won't be handled too"
Yes. It won't be handled. Because it runs out of CLR.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
Thanks N a v a n e e t h, 1. Seems UnhandledException is the only approach to handle exception from other threads, and I have tried even ProcessExit does not work. Here is my code to test. Could you review whether my code and my points are correct? :-) 2. After the handler for UnhandledException is executed, process is always terminated?
using System;
using System.Threading;namespace DelegateThread
{
class Test
{
private static void Method2 (object input)
{
Console.WriteLine("Method1 is throwing exception");
throw new ApplicationException("**** oops ****");
}private static void Method1() { Console.WriteLine("Method1 is throwing exception"); throw new ApplicationException("\*\*\*\* oops \*\*\*\*"); } private static void Handler1 (object sender, EventArgs e) { Console.WriteLine ("I am here"); } private static void Handler3(object sender, EventArgs e) { Console.WriteLine("I am here"); } delegate void Method1Delegate(); static void Main(string\[\] args) { Console.WriteLine("We first use a thread"); AppDomain.CurrentDomain.ProcessExit += new EventHandler (Test.Handler1); AppDomain.CurrentDomain.UnhandledException +=new UnhandledExceptionEventHandler(Test.Handler3); Thread aThread = new Thread(new ThreadStart(Method1)); aThread.Start(); Thread.Sleep(5000); Console.WriteLine("Survived exception"); return; } // main }
}
regards, George
-
Indeed, I always put a try/catch in the Main.
But you can never catch exception from other threads, PIEBALDconsult. :-) regards, George
-
Derek Bartram wrote:
Does anyone know if a try...catch block affects the performance of the try block code? I know it has a performance hit on hitting the block, but I wonder if it has a continuing effect beyond that.
I once worked on a project where the Project Lead insisted on try catch blocks in every bloody method. I did some performance testing and could determine definitive costs with the setup and teardown of the block, but was unable to measure any discernable difference to the code internally. However the code was entirely managed, perhaps wrapping unmanaged code has other implications.
I'm largely language agnostic
After a while they all bug me :doh:
What do you mean "unable to measure any discernable difference to the code internally"? regards, George
-
Thanks N a v a n e e t h, 1. Seems UnhandledException is the only approach to handle exception from other threads, and I have tried even ProcessExit does not work. Here is my code to test. Could you review whether my code and my points are correct? :-) 2. After the handler for UnhandledException is executed, process is always terminated?
using System;
using System.Threading;namespace DelegateThread
{
class Test
{
private static void Method2 (object input)
{
Console.WriteLine("Method1 is throwing exception");
throw new ApplicationException("**** oops ****");
}private static void Method1() { Console.WriteLine("Method1 is throwing exception"); throw new ApplicationException("\*\*\*\* oops \*\*\*\*"); } private static void Handler1 (object sender, EventArgs e) { Console.WriteLine ("I am here"); } private static void Handler3(object sender, EventArgs e) { Console.WriteLine("I am here"); } delegate void Method1Delegate(); static void Main(string\[\] args) { Console.WriteLine("We first use a thread"); AppDomain.CurrentDomain.ProcessExit += new EventHandler (Test.Handler1); AppDomain.CurrentDomain.UnhandledException +=new UnhandledExceptionEventHandler(Test.Handler3); Thread aThread = new Thread(new ThreadStart(Method1)); aThread.Start(); Thread.Sleep(5000); Console.WriteLine("Survived exception"); return; } // main }
}
regards, George
George_George wrote:
and I have tried even ProcessExit
ProcessExist is not needed here
George_George wrote:
After the handler for UnhandledException is executed, process is always terminated?
mm, look like you are still not clear. I will try to explain once more.
AppDomain.UnhandledException
is not an exception handler likecatch
. It's an event which will be fired before program exits due to uncaught error. After handler is executed, process will be terminated. This is a new behavior from .NET 2.0 onwards. In handler you can do necessary steps to log the error. You can't prevent application ending. In that handler you can show friendly messages to user and inform him that we are closing. Considering all these points in mind, your demo code is working as expected. Handler is getting executed and application is closing. You need to change thehandler3()
method like this.private static void Handler3(object sender, UnhandledExceptionEventArgs e)
{
Exception exceptionOccured = e.ExceptionObject as Exception;
string errorMessage = exceptionOccured.Message;
Console.WriteLine("I am here");
}. In this you can see how exception occurred is retrieved from the event argument. You can log the exception message and application will be exited gracefully. .NET 1.1 behavior can be taken back by setting some flag value in application.config file. See this[^]. But I don't recommend that, as I don't know the pros/cons of that. Hope it's clear now.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Thanks Spacix, 1.
Spacix One wrote:
If you wanted to go that far you can catch errors in your thread method also...
So, no means to catch exception from another thread, right? :-) 2.
Spacix One wrote:
on error resume next type functionally? If so might be better to use VB.NET as C# doesn't allow for that coding horror
Sorry, I have no experience in VB. Could you say something alternative to describe your ideas please? I do not quite understand, especially what do you mean "on error resume next type functionally". :-) regards, George
George_George wrote:
I do not quite understand, especially what do you mean "on error resume next type functionally".
It's a VB syntax used to resume the processing on errors. If
on error resume next
is provided on the code, errors will be skipped. Keep it in mind, this is for VB/VB.NET and not for C#.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Thanks Spacix, 1.
Spacix One wrote:
If you wanted to go that far you can catch errors in your thread method also...
So, no means to catch exception from another thread, right? :-) 2.
Spacix One wrote:
on error resume next type functionally? If so might be better to use VB.NET as C# doesn't allow for that coding horror
Sorry, I have no experience in VB. Could you say something alternative to describe your ideas please? I do not quite understand, especially what do you mean "on error resume next type functionally". :-) regards, George
N a v a n e e t h answered #2 As for number one you can catch them in that thread, example:
System.Threading.Thread mythread = new System.Threading.Thread
(delegate()
{
try
{
throw new Exception("Oh no, my thread crashed!");
}
catch(Exception err)
{
File.WriteAllText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\error.log",
DateTime.Now.ToString() + Environment.NewLine + err.ToString() + Environment.NewLine + Environment.NewLine);
}
});
mythread.Start();
-Spacix All your skynet questions[^] belong to solved
-
George_George wrote:
and I have tried even ProcessExit
ProcessExist is not needed here
George_George wrote:
After the handler for UnhandledException is executed, process is always terminated?
mm, look like you are still not clear. I will try to explain once more.
AppDomain.UnhandledException
is not an exception handler likecatch
. It's an event which will be fired before program exits due to uncaught error. After handler is executed, process will be terminated. This is a new behavior from .NET 2.0 onwards. In handler you can do necessary steps to log the error. You can't prevent application ending. In that handler you can show friendly messages to user and inform him that we are closing. Considering all these points in mind, your demo code is working as expected. Handler is getting executed and application is closing. You need to change thehandler3()
method like this.private static void Handler3(object sender, UnhandledExceptionEventArgs e)
{
Exception exceptionOccured = e.ExceptionObject as Exception;
string errorMessage = exceptionOccured.Message;
Console.WriteLine("I am here");
}. In this you can see how exception occurred is retrieved from the event argument. You can log the exception message and application will be exited gracefully. .NET 1.1 behavior can be taken back by setting some flag value in application.config file. See this[^]. But I don't recommend that, as I don't know the pros/cons of that. Hope it's clear now.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
Thanks N a v a n e e t h, Cool! I have made further tests that, there is one exception case. When exception is from thread pool thread -- but in the situation of executing asynchronous method call, we can catch the exception (even if unhandled in the thread pool worker thread) in EndInvoke in main thread. So, here is a case when there is unhandled exception in another thread, we can still catch it and not make process terminated. :-) Any comments? regards, George