Restart an application after exception

LookitsPuck

Active member
Joined
Jan 23, 2007
Messages
37
Programming Experience
3-5
Is there a way through the AppDomain's UnexpectedExceptionHandler function to restart the application? I'd like to this rather than through a scheduled task.

Thanks,
-Steve
 
Much obliged. This will execute regardless of the fact that the execution was thrown?

BTW, an exception is throwing and I'm trying to figure out in what part of the code. Problem is, with 2.0, the application closes too fast for me to figure out where the problem is. I have the UnexpectedExceptionHandler executing, and unfortunately what I cast the args.ExceptionObject to an Exception, I only get a simple exception message, not the entire exception message that is printed to the console. Do you know of any way to grab that entire message?
 
Why dont you run it in the debugger, with "Break on Thrown" rather than "Break on Un-handled" set? (See Debug Menu, Exceptions..)
 
BTW, an exception is throwing and I'm trying to figure out in what part of the code. Problem is, with 2.0, the application closes too fast for me to figure out where the problem is. I have the UnexpectedExceptionHandler executing, and unfortunately what I cast the args.ExceptionObject to an Exception, I only get a simple exception message, not the entire exception message that is printed to the console. Do you know of any way to grab that entire message?

It is important for us developers to not just us a MsgBox to display exceptions and hope to capture them from our users but also to build in a good error logging system for your troubleshooting. .NET 2.0 has much improved functionality to help you, research TraceListener and/or consider your own home grown logging system that captures exceptions to a file as an example.
 
Oh, I know, I log them. :)

Problem is, I'm not throwing the exceptions, they're unhandled (unexpected), so I'm trying to figure out what's causing the issue. It's most likely a synchronization issue, as this is a multithreaded socket server.
 
BTW, I can't use Application.Restart, can I? This is not a Windows Forms application. It's a Windows Console application.
 
I went along with Process.Start to start back up the application. Used reflection to get the name of the executing assembly.
 
I'm not throwing the exceptions, they're unhandled (unexpected)

So why dont you run a debug version of the app that breaks on throw, rather than breaks on unhandled and then handle them... In .NET we dont just write exception handlers for our own-thrown exceptions, as Im sure you know..

If you catch an exception when its thrown rather than when it bubbles, you dont lose info (like JohnH had recently, when a delegate invoke was throwing an error, the exception bubble eventually killed the app with "Invokation target threw an exception" - not very helpful)..

For how to catch exceptions when theya re thrown, see my previous post, this thread..
 
Alright, that sounds good. :) I was just wondering if there was a sway to get the stack trace of an unhandled exception, but running in debug may be just as good (albeit it a bit slower).

So what you're saying, if I run in debug mode (just have to change the app.config to debug, right?), and compile with the checks as Thrown instead of Unhandled, then the app will continue running, and will give me an idea of where the exception is occuring?

Can you step by step what I have to do? I've debugged before and caught problems via that, just never as you told.

Much appreciated. :)
 
You go into Debug menu.. Exceptions. Tick "Unhandled" for CLR exceptions, then press Play.. and run the app in the IDE (VS2005).. If its a server, then reconfigure the DNS or something, to get all the connections to hit your dev machine isntead of the real server

As soon as it breaks, the code will halt at the point that the code encountered the problem. You can then interrogate and fix the error.
 
Hmmm, not sure if I can do that. I develop locally. Can't point the DNS to this box.

Is there a way to do remote debugging? Or is that too complicated?
 
Then you'll need to simulate it - write a program that launches thousands of connections, or use logs to assess what kind of data is being passed at the point the server falls over..
 
Right. Going to be fun simulating this. :) I wonder if I can get one of the UI guys to somehow simulate this.

Anyway, one last question.

I have a function:

VB.NET:
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] UnexpectedExceptionHandler([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] args [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] UnhandledExceptionEventArgs)
[/SIZE][SIZE=2][COLOR=#008000]'Write to log
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] exception [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](args.ExceptionObject, Exception)
WriteLog(exception.Message & [/SIZE][SIZE=2][COLOR=#800000]" - number of exceptions: "[/COLOR][/SIZE][SIZE=2] & numExceptions)
numExceptions += 1
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] (numExceptions = 1) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]GC.Collect()
GC.WaitForPendingFinalizers()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] asm [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Assembly = Assembly.GetExecutingAssembly()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] asmName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = asm.GetName().Name() & [/SIZE][SIZE=2][COLOR=#800000]".exe"
[/COLOR][/SIZE][SIZE=2]Process.Start(System.Environment.CurrentDirectory & [/SIZE][SIZE=2][COLOR=#800000]"\"[/COLOR][/SIZE][SIZE=2] & asmName)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

That takes care of the unhandled exceptions, and restarts the application. Problem is, there are about 3 exceptions that are thrown at the same time that causes the application to crash (I write these to a log as you can see). So, I definitely don't want the server to open up 3 (or however many) times every time the server throws an exception.

Is what I'm doing correct? This is the first time I'm trying to simulate an automatic restart (without me manually closing the app through the console). Keep in mind this is a console application and not a windows form app.

Thanks,
-Steve
 
I dont see how it's possible for an application to simultaneously experience 3 halting events - as soon as the first exception happens, it bubbles out unhandled and crashes the app - there should be no app left to experience the other 2.. The other thing is.. if you catch an exception and restart the app, you MUST be able to catch an exception and have the existing app survive it.
 
Back
Top