Silent Failures in C#, Seemingly Unhandled Exceptions That Does Not Crash the Program

Silent failures in C#, seemingly unhandled exceptions that does not crash the program

This is a known problem on x64 systems:

This is a known issue on 64-bit OS
platform. The reason is that the 64bit
OS core does not allow user mode
exception through kernal mode stacks.
The exception is swallowed by OS
sliently. That happens in FormLoad
handler, because it is called in an OS
callback. 32bits OS doesn't do this,
so it doesn't repro there.

The OS team is investigating related
issues. In the mean time, you do have
to work around this issue. Turning on
"Stop on first chance exception" will
make the debugger to stop in this
scenario. But it does make the
debugger to stop very often, so you
might want to do this only when you
find a problem.

The linked bug report was last updated February 2008, and doesn't indicate what's happened since then.

I can reproduce most poster's behavior on my 32-bit system here, and I can reproduce the OP's behavior on my 64-bit (Vista SP2, 3.5SP1 Framework) work PC.

Unable to capture Unhandled Exceptions in Winforms

Try disabling exception catching in VS, as it seems to catch the exception before it gets to your handlers.

Debug > Exceptions... > Uncheck User-unhandled for Common Language Runtime Exceptions.

How come if I throw an exception on the Form_Load event of a Windows Form, nothing happens?

If you run it outside the debugger it is thrown. You need to configure the debugger to break on the un-handled exception.

Debug->Exceptions...
Common Language Runtime Exceptions
System
System.Exception Click the "Thrown" checkbox.

Method exits without exception when trying to access first element of an empty collection (Edit: Known Form Load issue)

No, that will fail with an InvalidOperationException. I'm sure you're just catching the exception in the calling code. It's very easy to show - just take your exact code and put it into a short but complete sample:

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

class Test
{
static void Main()
{
var emptyList = new List<string>();
var aha = emptyList.Where(i => i == "four");
var props = aha.First();
}
}

Result:

Unhandled Exception: System.InvalidOperationException: Sequence contains no elements
at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
at Test.Main()

So your next step is to work out why you're not seeing the exception - and we can't help with that.

Use Delphi Dll and some problems

You cannot expect to call that function from a programming environment other than Delphi. That's because it uses Delphi native strings which are not valid for interop. Even if you call from Delphi you need to use the same version of Delphi as was used to compile the DLL, and the ShareMem unit so that the memory manager can be shared. That function is not even well designed for interop between two Delphi modules.

You need to change the DLL function's signature. For example you could use:

procedure CryptStr(
str: PAnsiChar;
Key: PAnsiChar;
DecryptStr: boolean;
output: PAnsiChar;
); stdcall;

In C# you would declare this like so:

[DllImport("Crypt2.dll")]
static extern void CryptStr(
string str,
string Key,
bool DecryptStr,
StringBuilder output
);

This change requires the caller to allocate the buffer that is passed to the function. If you want to find examples of doing that, search for examples calling the Win32 API GetWindowText.

If you were using UTF-16 text instead of 8 bit ANSI, you could use COM BSTR which is allocated on the shared COM heap, but I suspect that option is not available to you.

As for your program not showing any errors, I refer you to these posts:

  • http://blog.paulbetts.org/index.php/2010/07/20/the-case-of-the-disappearing-onload-exception-user-mode-callback-exceptions-in-x64/
  • http://blog.adamjcooper.com/2011/05/why-is-my-exception-being-swallowed-in.html
  • Silent failures in C#, seemingly unhandled exceptions that does not crash the program

Sub-routines being skipped during form load

What I have seen, is that in some event handlers (maybe like your Form.Load handler), any exception that is thrown during execution will simply be swallowed, and ignored. Most likely you are getting some exception in your OleDb code, that is causing it to bail out.

I would recommend wrapping all of your _Load subroutine with a Try...Catch block, and manually printing out the exception, or calling Debugger.Break.

Have you tried stepping through the code? I would recommend that you set a breakpoint at the beginning of frmEnroll_Load, and start stepping through until something blows up, or the code just continues (which is what you would see if the exception was getting swallowed.)


Related questions / pages:

  • Silent failures in C#, seemingly unhandled exceptions that does not crash the program
  • Why the form load can't catch exception?
  • Why is my exception being swallowed in my Form’s Load handler?


Related Topics



Leave a reply



Submit