Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / July 2003

Tip: Looking for answers? Try searching our database.

Exception on Application

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Harkos - 28 Jul 2003 16:34 GMT
I'm currently trying to make my application restart when an unhandled
exception gets through to the Main call. It does something like this:

do
 try {
   Application.Run(new Form1( ));
   break;
 } catch(Exception e) {
   // Here I can create that report to send and
 }
while(true);

My problem is with the try/catch block. I'm developing and testing with .NET
Framework 1.1 on different machines. On the development machine (Win2k
Server) it runs just fine. On the test machine (Win2k Pro) it shows a
default error message. What am I doing wrong.

[]'s,
Harkos
Jay B. Harlow [MVP - Outlook] - 28 Jul 2003 17:11 GMT
Harkos,
Wrapping your Application.Run method with a Try/Catch block will not catch
unhandled exceptions.

The events occur asynchronously, in response to mouse clicks & other user or
system actions, if you were to catch these after Application.Run your app
would have stopped running  (Application.Run is a 'Message Pump' loop, you
do not want that loop to exit, unless you are exiting your app).

If you want to avoid the window that says there was an unhandled exception,
you can use global exception handlers in your .NET app.

Depending on the type of application you are creating, .NET has three
different global exception handlers.

For ASP.NET look at:
   System.Web.HttpApplication.Error event
   Normally placed in your Global.asax file.

For console applications look at:
   System.AppDomain.UnhandledException event
   Use AddHandler in your Sub Main.

For Windows Forms look at:
   System.Windows.Forms.Application.ThreadException event
   Use AddHandler in your Sub Main.

In stead of the do try/catch loop, simply handle the
Application.ThreadException event before calling other methods on
Application.

Hope this helps
Jay

> I'm currently trying to make my application restart when an unhandled
> exception gets through to the Main call. It does something like this:
[quoted text clipped - 15 lines]
> []'s,
> Harkos
Harkos - 28 Jul 2003 18:22 GMT
Hi Jay,

I'm developing a Windows Form application. What I'm trying to create is the
same thing that happens with MSIE when a huge error happens. I'd rather
really close the entire app and restart it to avoid creating a much bigger
problem to the user. So I don't simply want to handle the exception, but
also restart the application and check if the user wants to send me a report
about the error.

Another thing, why my code runs correctly on Win2K Server?

[]'s,
Harkos

> Wrapping your Application.Run method with a Try/Catch block will not catch
> unhandled exceptions.
[quoted text clipped - 7 lines]
>     System.Windows.Forms.Application.ThreadException event
>     Use AddHandler in your Sub Main.
Jay B. Harlow [MVP - Outlook] - 29 Jul 2003 15:31 GMT
Harkos,
> Another thing, why my code runs correctly on Win2K Server?
Not sure.

> So I don't simply want to handle the exception, but
> also restart the application and check if the user wants to send me a report
> about the error.

I would not put the Application.Run in simple loop as you did then. I would
actually create new AppDomain and really restart the "app". This is what
ASP.NET does. Unfortunately I do not have a specific example of this. You
might start here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cp
conapplicationdomains.asp


As simply restarting Application.Run, you will still have objects in memory
that you are using, static/shared variables & such. Where as creating a new
AppDomain will cause all objects to be created fresh.

The next level of isolation would be to actually restart the app. Actually
have 2 programs. The 'monitor' that uses Process.Start to start the actual
app. If the monitor decides the actual app failed, it would use
Process.Start to physically restart the app.

Hope this helps
Jay

> Hi Jay,
>
[quoted text clipped - 22 lines]
> >     System.Windows.Forms.Application.ThreadException event
> >     Use AddHandler in your Sub Main.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.