Try Catch Not Working on BadImageFormatException

I have an MVC application that loads an external DLL, and when I'm in production, I don't get any errors at all. Firefox just says the connection was reset. So I put some try / catch in the code, but they still do not work, I still get a reset message.

I know the error is a BadImageFormatException exception, but why can't I see anything in the browser?

 public class HomeController : Controller
    {

        [DllImport("CDCrypt.dll")]
        [return: MarshalAs(UnmanagedType.LPStr)]
        public static extern String Encrypt([MarshalAs(UnmanagedType.LPStr)] String aName);



        [DllImport("CDCrypt.dll")]
        [return: MarshalAs(UnmanagedType.LPStr)]
        public static extern String Decrypt([MarshalAs(UnmanagedType.LPStr)] String aName);


        //
        // GET: /Home/

        public ActionResult Index()
        {

            try
            {
                ViewBag.EncryptString = Encrypt("test");
            }
            catch (Exception e)
            {
                ViewBag.EncryptString = "Stack Trace\r\n:" + "\r\nException: " + e.Message;
                return new HttpStatusCodeResult(500);
            }
            return View();
        }

        public ActionResult Up()
        {
            ViewBag.Up = "You can see me";
            return View();
        }



    }
+3
source share
2 answers

There seem to be some exceptions that are flagged as fatal and therefore cannot be caught. This question (well, this answer really) has a list of them, but I don’t know how exhaustive it is.

, , , , runtime .

, catch, catch(Exception ex) COM. , ( ), .

+4

All Articles