简体   繁体   中英

Strange behavior - error happens but exception isn't thrown

Here's what I have:

Debug.WriteLine("myMethod ok");
db.myTable.AddObject(new_item);
db.SaveChanges();
Debug.WriteLine("myMethod save changes");

And in the output window, I get:

"myMethod ok"

and nothing after that. So, AddObject and SaveChanges don;t throws exception because if they would, I would get that exception when I debug.

What could be the reason for this?

Re-reading your question, I realized it also could be a different issue. Perhaps db.SaveChanges(); hangs forever, Put a break point in at the top of the function and step through.


The exception may be being caught by a higher level try-catch block that is calling your function.

You can follow what is happening by enabling "Catch Exceptions on Thrown" by going to the Debug->Execptions... dropdown menu and checking the Thrown box for Common Language Runtime Exceptions.

在此处输入图片说明

This will cause your code to break on the exception, even if it is inside a try-catch block.

If you do not see the box for "User-Unhanded" to check like the below picture

在此处输入图片说明

go to Debug -> Options and Settings... and be sure the box for "Enable Just My Code" is checked.

在此处输入图片说明

If it is not checked it will break on internal exceptions thrown by the .NET framework that do not represent problems, just control of flow logic that is implemented via exceptions in the framework (Most of the time it is a TimeOutExecption of some kind).

If the exception is still not being caught, try it again with "Just My Code" unchecked, just be aware of the possible "red-herring" exceptions the framework may throw.

Check menu DEBUG - Exceptions and mark all Thrown checkboxes. Maybe than you will receive the Exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM