Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. UnhandledException

UnhandledException

Scheduled Pinned Locked Moved C#
question
13 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Hamid Taebi

    Does anyone know why this code doenst work?(In a consonle Application) it cant catch exceptions static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyException); throw new ArgumentException("for test"); } static void MyException(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine(e.ExceptionObject); } } Thanks

    Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #2

    What do you mean "doesn't work"? I just tried it and it does what I would expect.

    Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    H 1 Reply Last reply
    0
    • H Hamid Taebi

      Does anyone know why this code doenst work?(In a consonle Application) it cant catch exceptions static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyException); throw new ArgumentException("for test"); } static void MyException(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine(e.ExceptionObject); } } Thanks

      Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #3
          static void Main(string\[\] args)
          {            
              AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyException);
      
              throw new ArgumentException("for test");
          }
      
          static void MyException(object sender, UnhandledExceptionEventArgs e)
          {
              Console.WriteLine("CAUGHT: " + e.ExceptionObject.ToString());
          }
      

      Works for me; displays the exception with the custom text in the console, and then switches to the debugger. What did you expect it to do, and what behaviour are you observing?

      Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

      H 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        What do you mean "doesn't work"? I just tried it and it does what I would expect.

        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

        H Offline
        H Offline
        Hamid Taebi
        wrote on last edited by
        #4

        Im wonderful it cant catgh any unhandled exceptions and clr shows unhandled exception windows when I see event viewer it shows these info about my program Faulting application name: Mydynamic.exe, version: 1.0.0.0, time stamp: 0x507ac6d7 Faulting module name: KERNELBASE.dll, version: 6.1.7601.17514, time stamp: 0x4ce7bafa Exception code: 0xe0434352 Fault offset: 0x0000b727 Faulting process id: 0x4b4 Faulting module path: F:\Windows\syswow64\KERNELBASE.dll Report Id: 81b9c3ff-1608-11e2-bc0d-005056c00008 I dont have this problem in other programs(like WPF,Windows Forms or ASP.Net) it works for them and donest work for console applications

        Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

        OriginalGriffO 1 Reply Last reply
        0
        • L Lost User
              static void Main(string\[\] args)
              {            
                  AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyException);
          
                  throw new ArgumentException("for test");
              }
          
              static void MyException(object sender, UnhandledExceptionEventArgs e)
              {
                  Console.WriteLine("CAUGHT: " + e.ExceptionObject.ToString());
              }
          

          Works for me; displays the exception with the custom text in the console, and then switches to the debugger. What did you expect it to do, and what behaviour are you observing?

          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

          H Offline
          H Offline
          Hamid Taebi
          wrote on last edited by
          #5

          Its the result of event viewver Faulting application name: Mydynamic.exe, version: 1.0.0.0, time stamp: 0x507ac6d7 Faulting module name: KERNELBASE.dll, version: 6.1.7601.17514, time stamp: 0x4ce7bafa Exception code: 0xe0434352 Fault offset: 0x0000b727 Faulting process id: 0x4b4 Faulting application start time: 0x01cdaa154403b39d Faulting application path: c:\users\apple\documents\visual studio 2010\Projects\Mydynamic\Mydynamic\bin\Debug\Mydynamic.exe Faulting module path: F:\Windows\syswow64\KERNELBASE.dll Report Id: 81b9c3ff-1608-11e2-bc0d-005056c00008 but I dont know why the path is c:\ all my programs are on f:\

          Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

          L 1 Reply Last reply
          0
          • H Hamid Taebi

            Its the result of event viewver Faulting application name: Mydynamic.exe, version: 1.0.0.0, time stamp: 0x507ac6d7 Faulting module name: KERNELBASE.dll, version: 6.1.7601.17514, time stamp: 0x4ce7bafa Exception code: 0xe0434352 Fault offset: 0x0000b727 Faulting process id: 0x4b4 Faulting application start time: 0x01cdaa154403b39d Faulting application path: c:\users\apple\documents\visual studio 2010\Projects\Mydynamic\Mydynamic\bin\Debug\Mydynamic.exe Faulting module path: F:\Windows\syswow64\KERNELBASE.dll Report Id: 81b9c3ff-1608-11e2-bc0d-005056c00008 but I dont know why the path is c:\ all my programs are on f:\

            Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            Hamid. wrote:

            Its the result of event viewver

            Doesn't contain much hints on what went wrong. I hope you're logging the full text of the exceptions?

            Hamid. wrote:

            Faulting application path: c:\users\apple\documents\visual studio 2010\Projects\Mydynamic\Mydynamic\bin\Debug\Mydynamic.exe
            Faulting module path: F:\Windows\syswow64\KERNELBASE.dll
            Report Id: 81b9c3ff-1608-11e2-bc0d-005056c00008
            but I dont know why the path is c:\ all my programs are on f:\

            Looks like it's (also) being run from Visual Studio, debugging on it's C-drive.

            Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

            H 1 Reply Last reply
            0
            • H Hamid Taebi

              Im wonderful it cant catgh any unhandled exceptions and clr shows unhandled exception windows when I see event viewer it shows these info about my program Faulting application name: Mydynamic.exe, version: 1.0.0.0, time stamp: 0x507ac6d7 Faulting module name: KERNELBASE.dll, version: 6.1.7601.17514, time stamp: 0x4ce7bafa Exception code: 0xe0434352 Fault offset: 0x0000b727 Faulting process id: 0x4b4 Faulting module path: F:\Windows\syswow64\KERNELBASE.dll Report Id: 81b9c3ff-1608-11e2-bc0d-005056c00008 I dont have this problem in other programs(like WPF,Windows Forms or ASP.Net) it works for them and donest work for console applications

              Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #7

              My complete test code:

              using System;
              using System.Collections.Generic;
              using System.Linq;
              using System.Text;

              namespace ConsoleApplication1
              {
              class Program
              {
              static void Main(string[] args)
              {
              AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyException);

                      throw new ArgumentException("for test");
                      }
              
                  static void MyException(object sender, UnhandledExceptionEventArgs e)
                      {
                      Console.WriteLine("Exception Caught!");
                      Environment.Exit(0);
                      }
                  }
              }
              

              My complete CMD output:

              Microsoft Windows [Version 6.1.7601]
              Copyright (c) 2009 Microsoft Corporation. All rights reserved.

              C:\Users\griff>d:

              D:\>cd \temp

              D:\Temp>ca
              Exception Caught!

              D:\Temp>

              What am I doing that differs from you?

              Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              H 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                My complete test code:

                using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;

                namespace ConsoleApplication1
                {
                class Program
                {
                static void Main(string[] args)
                {
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyException);

                        throw new ArgumentException("for test");
                        }
                
                    static void MyException(object sender, UnhandledExceptionEventArgs e)
                        {
                        Console.WriteLine("Exception Caught!");
                        Environment.Exit(0);
                        }
                    }
                }
                

                My complete CMD output:

                Microsoft Windows [Version 6.1.7601]
                Copyright (c) 2009 Microsoft Corporation. All rights reserved.

                C:\Users\griff>d:

                D:\>cd \temp

                D:\Temp>ca
                Exception Caught!

                D:\Temp>

                What am I doing that differs from you?

                Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                H Offline
                H Offline
                Hamid Taebi
                wrote on last edited by
                #8

                My code is the same with your code except (Environment.Exit(0); (I dont have this code) it works with this code but without it I got this message of e.ExceptionObject System.ArgumentException: for test at ConsoleApplication1.Program.Main(String[] args) in c:\users\apple\documents\visual studio 2010\Projects\Mydynamic\Mydynamic\Program.cs:line 17 and it shows this window Windows can check online for a solution to the problems. Check online for a solution and close the program Close the program Debug the problem when I click on the "Debug the problem" item, visual sutdio Just In-Time debugger asks "Do you want to debug using the selected debugger?" after yes it goes to my code exception details are System.ArgumentException was unhandled Message=for test Source=Mydynamic StackTrace: at ConsoleApplication1.Program.Main(String[] args) in c:\users\apple\documents\visual studio 2010\Projects\Mydynamic\Mydynamic\Program.cs:line 17 InnerException:

                Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

                OriginalGriffO 1 Reply Last reply
                0
                • L Lost User

                  Hamid. wrote:

                  Its the result of event viewver

                  Doesn't contain much hints on what went wrong. I hope you're logging the full text of the exceptions?

                  Hamid. wrote:

                  Faulting application path: c:\users\apple\documents\visual studio 2010\Projects\Mydynamic\Mydynamic\bin\Debug\Mydynamic.exe
                  Faulting module path: F:\Windows\syswow64\KERNELBASE.dll
                  Report Id: 81b9c3ff-1608-11e2-bc0d-005056c00008
                  but I dont know why the path is c:\ all my programs are on f:\

                  Looks like it's (also) being run from Visual Studio, debugging on it's C-drive.

                  Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                  H Offline
                  H Offline
                  Hamid Taebi
                  wrote on last edited by
                  #9

                  Im using of visual studio 2010 SP1 I got this message of e.ExceptionObject System.ArgumentException: for test at ConsoleApplication1.Program.Main(String[] args) in c:\users\apple\documents\visual studio 2010\Projects\Mydynamic\Mydynamic\Program.cs:line 17 and it clr shows this window Windows can check online for a solution to the problems. Check online for a solution and close the program Close the program Debug the problem when I click on the "Debug the problem" item, visual sutdio Just In-Time debugger asks "Do you want to debug using the selected debugger?" after yes it goes to my code exception details are System.ArgumentException was unhandled Message=for test Source=Mydynamic StackTrace: at ConsoleApplication1.Program.Main(String[] args) in c:\users\apple\documents\visual studio 2010\Projects\Mydynamic\Mydynamic\Program.cs:line 17 InnerException:

                  Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

                  L 1 Reply Last reply
                  0
                  • H Hamid Taebi

                    My code is the same with your code except (Environment.Exit(0); (I dont have this code) it works with this code but without it I got this message of e.ExceptionObject System.ArgumentException: for test at ConsoleApplication1.Program.Main(String[] args) in c:\users\apple\documents\visual studio 2010\Projects\Mydynamic\Mydynamic\Program.cs:line 17 and it shows this window Windows can check online for a solution to the problems. Check online for a solution and close the program Close the program Debug the problem when I click on the "Debug the problem" item, visual sutdio Just In-Time debugger asks "Do you want to debug using the selected debugger?" after yes it goes to my code exception details are System.ArgumentException was unhandled Message=for test Source=Mydynamic StackTrace: at ConsoleApplication1.Program.Main(String[] args) in c:\users\apple\documents\visual studio 2010\Projects\Mydynamic\Mydynamic\Program.cs:line 17 InnerException:

                    Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #10

                    That is pretty much what I would expect. If you don't have the exception handler cause an program exit, then the system has no choice except to cause an abnormal termination - which is what your message shows. Think about it: the exception is thrown from the Main method. The application cannot continue under any circumstances without restarting the whole program. In Winforms et al., it is multithreaded, so a single thread can be terminated without necessarily killing the whole app - but your console app is a single thread. If it can't continue the only running thread, it can't continue the application. In GUI apps, the Main function is executed in a thread, which fires off other threads to handle the UI for each form. They can be terminated without affecting the Main thread, since all it is doing is waiting for the others to die anyway! :laugh:

                    Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    H 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      That is pretty much what I would expect. If you don't have the exception handler cause an program exit, then the system has no choice except to cause an abnormal termination - which is what your message shows. Think about it: the exception is thrown from the Main method. The application cannot continue under any circumstances without restarting the whole program. In Winforms et al., it is multithreaded, so a single thread can be terminated without necessarily killing the whole app - but your console app is a single thread. If it can't continue the only running thread, it can't continue the application. In GUI apps, the Main function is executed in a thread, which fires off other threads to handle the UI for each form. They can be terminated without affecting the Main thread, since all it is doing is waiting for the others to die anyway! :laugh:

                      Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                      H Offline
                      H Offline
                      Hamid Taebi
                      wrote on last edited by
                      #11

                      A question I am making a new thread so my program have two threads (main thread) and worker thread then I am testing this code but the problem is same ? could you explain it ? Thanks

                      Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

                      J 1 Reply Last reply
                      0
                      • H Hamid Taebi

                        A question I am making a new thread so my program have two threads (main thread) and worker thread then I am testing this code but the problem is same ? could you explain it ? Thanks

                        Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

                        J Offline
                        J Offline
                        jschell
                        wrote on last edited by
                        #12

                        Hamid. wrote:

                        I am making a new thread

                        Solution is simple. Don't let threads throw exceptions.

                        1 Reply Last reply
                        0
                        • H Hamid Taebi

                          Im using of visual studio 2010 SP1 I got this message of e.ExceptionObject System.ArgumentException: for test at ConsoleApplication1.Program.Main(String[] args) in c:\users\apple\documents\visual studio 2010\Projects\Mydynamic\Mydynamic\Program.cs:line 17 and it clr shows this window Windows can check online for a solution to the problems. Check online for a solution and close the program Close the program Debug the problem when I click on the "Debug the problem" item, visual sutdio Just In-Time debugger asks "Do you want to debug using the selected debugger?" after yes it goes to my code exception details are System.ArgumentException was unhandled Message=for test Source=Mydynamic StackTrace: at ConsoleApplication1.Program.Main(String[] args) in c:\users\apple\documents\visual studio 2010\Projects\Mydynamic\Mydynamic\Program.cs:line 17 InnerException:

                          Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #13

                          That seems to work as expected; the AppDomain-handler correctly gets the message and processes it. And no, it doesn't prevent the app from closing - you'll have to fix the exception.

                          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups