Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am getting access violation exception as Attempted to read or write protected memory. This is often an indication that other memory is corrupt.how to handle this exception and log the exception details in to text file.

i am getting error at application.run(new loadertest)

C#
[STAThread]
        static void Main()
        {
            AppDomain.CurrentDomain.UnhandledException +=new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.ThreadException +=new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new LoaderTest());
        }
Posted

1 solution

Handling it on this level will give your little help. System.Windows.Forms has a special mechanism of handling all exceptions in the main event loop, something you really need to use in all cases. I described it in my past answers:
Error Logging and Screen Shot.[^],
Catching an Exception[^],
Handling exceptions in class library (dll)[^].

(WPF has very similar mechanism.)

Now, on top of it, you may need to do exception handling on some other, very few, strategically chosen places, which I call "competency points". Sometime you need to re-throw then to later handling on different level. Besides, you need to catch all exceptions on top stack frame of each thread (unless you develop some mechanism similar to the one I described for forms). Please see my other past answers:
Exception Details: System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error:...[^],
When i run an application an exception is caught how to handle this?[^],
Image taken from Webcam not getting saved on the server[^],
throw . .then ... rethrowing[^].

Generally, exceptions should rarely be handled; in most cases, you should just let them propagate up, out of your current stack frame. This is designed to allow you to well isolate exceptional behavior from "regular". It's good to understand how exception works in general. They are like a kind of time machine. I tries to explain it here:
Does Exception in C# Constructor Cause Caller Assignment to Fail?[^],
where was stored .net exceptions in operating system[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900