473,327 Members | 1,976 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

how do you catch unhandle exception in .net?

how do you catch unhandle exception in .net?
Jun 15 '11 #1
1 1256
alexis4
113 100+
Hi!

I have done this once in C#, VS2008.

Below is the file program.cs. There is an exception handler for thread exception and another one for unhandled exception. The code is tested, exceptions actually cathed by this code. After the exception, application is terminated.


Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace GameSelector
  5. {
  6.     static class Program
  7.     {
  8.         /// <summary>
  9.         /// The main entry point for the application.
  10.         /// </summary>
  11.         [STAThread]
  12.         static void Main()
  13.         {
  14.             AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  15.             Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
  16.  
  17.             Application.EnableVisualStyles();
  18.             Application.SetCompatibleTextRenderingDefault(false);
  19.             Application.Run(new MainForm());
  20.         }
  21.  
  22.         static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  23.         {//In this function you put your exception code, below is the code I wrote for the exception
  24.             GameSelector.MainForm.sss = GameSelector.MainForm.unh_exc;
  25.             GameSelector.MainForm.unh_exc = "unhexc";
  26.             try
  27.             {
  28.                 GameSelector.MainForm.unlock_all__();
  29.                 Exception ex = (Exception)e.ExceptionObject;//declare exception
  30.  
  31.                 //Important message box, tells you the serial number of exception (unh_exc)
  32. MessageBox.Show(GameSelector.MainForm.unh_exc + GameSelector.MainForm.sss
  33.                       + " information:\n\n" + ex.Message + ex.StackTrace,
  34.                       "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  35.             }
  36.             finally
  37.             {
  38.                 Application.Exit();
  39.             }
  40.         }
  41.  
  42.         public static void Application_ThreadException (object sender, System.Threading.ThreadExceptionEventArgs e)
  43.         {//same for thread exception
  44.             GameSelector.MainForm.sss = GameSelector.MainForm.unh_exc;
  45.             GameSelector.MainForm.unh_exc = "unhexc";
  46.             try
  47.             {
  48.                 GameSelector.MainForm.unlock_all__();
  49.                 //Exception ex = (Exception)e.ExceptionObject;
  50.                 //result = 
  51.                 MessageBox.Show(GameSelector.MainForm.unh_exc + GameSelector.MainForm.sss + 
  52.                     e.Exception.Message + e.Exception.StackTrace, "Application Error");
  53.                 //, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
  54.  
  55.                 /*MessageBox.Show(GameSelector.MainForm.unh_exc + GameSelector.MainForm.sss
  56.                       + " information:\n\n" + ex.Message + ex.StackTrace,
  57.                       "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);*/
  58.             }
  59.             finally
  60.             {
  61.                 Application.Exit();
  62.             }
  63.         }
  64.     }
  65. }

Hope that helped,
Alexis.
Jun 15 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: eas | last post by:
How to catch the following exceptions? Failure in an assignment operator Detection of an out-of-bound index in a user-defined array Range-checked access to a vector Any comments are...
7
by: Dan Bass | last post by:
In a somewhat complex application, I've developed plug-in architecture and am having a problem as to when to catch general exceptions for logging purposes. In each plug-in class library, for...
3
by: will | last post by:
Hi all. I've got an question about how to catch an exception. In Page_Load, I place a DataGrid, dg1, into edit mode. This will call the method called GenericGridEvent. GenericGridEvent will call...
5
by: Roy | last post by:
Hi all, I'm getting an error messagebox that displays for less than a second and disappers. I found the line that causing this error, and enclosed in a try block, but I'm not able to catch this...
1
by: Paul Wu | last post by:
Suppose I have a form (frmMain) and a class (CheckStatus) that is instantiated on Form_Load. A button on the frmMain calls the CheckStatus.StartTimer method. The timer fires every second and...
5
by: Saizan | last post by:
In an event-driven application i'd like to keep the program alive regardless of any exceptions raised by the handlers, but still be able to debug them by reading the appropriate TraceBack from...
3
by: LMzizi | last post by:
Hi, I greated a application using vb.net 2005, my problem is that when i run it in the machine that have no vb.net it shows a message reading like this, (System InvalidOperationException: An error...
3
by: yinglcs | last post by:
I read the document here about exception handling in python: http://www.diveintopython.org/file_handling/index.html Can you please tell me how can I catch all exception in python? like this in...
12
by: Angus | last post by:
Hello I have a class I am using which raises an exception in its constructor if certain things aren't in place. I can easily create the situation where an exception is raised. If I use the...
1
Dormilich
by: Dormilich | last post by:
I was wondering, where I should catch an exception thrown from a function that is executed onevent… // let’s say we have an AJAX call XHR.onreadystatechange = function () { if (4 ===...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.