.NET exception thrown

I have a function that looks like this:

try { _dbManager.InsertSearch(some data); } catch (Exception ex) { //do logging } 

_dbManager uses LINQ to insert data into an SQL database. Yesterday, the machine hosting the database ended up with a lack of hard disk space, and my program crashed. I have a crash dump that shows that there was a SqlClient.SqlException with an exception message reading something like "Database transaction log is full ...".

My question is: Why did the exception not fall into the catch block above? Strange, when I tried to reproduce the problem, I could get the same exception, but this was caught by the catch block. What could be the problem?

Secondly, a related question: Imagine if we use a third-party library and we do not want an exception to occur. We can use a try-catch block, but this only works when a thread is called. What if a third party starts a new thread and an exception occurs there? Is there any way to handle this? I know that I can register our UnhandledExceptionHandler , but it looks like what I wanted.

+6
source share
3 answers

My question is: why did the exception not fall into the catch block above?

As David Stratton suggests, the system might not be on disk and could not write a log file. There is also the possibility that the process was interrupted due to a Corrupted State Exception that will not be delivered to your catch-all block in .NET 4. The exception that terminated the process may also have been thrown from a thread that did not share.

Secondly, a related question: imagine if we use a third-party library and we don’t want any thrown throw.

I think you have to stop right now and rethink it. You say that you are absolutely 100% sure that nothing can go wrong in the third-party library. There are certain exceptions (such as OutOfMemoryException ) that you should not catch, because your code simply does not know how to recover them. The rule of thumb with exception handling is that you should use only those exceptions that you fully understand and know how to recover from . Please take a look at this answer and the links in it.

What if a third party starts a new thread and throws an exception? there? Is there any way to handle this?

The best way to handle this is to rely on the default CLR policy, which will terminate your application. The only reasonable thing you can do is try to register it by subscribing to AppDomain.UnhandledException .

+6
source

Is it possible that inside this method there is a try/catch another try/catch that has a conditional throw statement.

The developer of the DBManager class chose not throw if there is not enough disk space for recording. Just a thought.

0
source

If you use code contract validation, I advise you to check the compiled version of your DLL. Read this for details. Why is the .net exception not found?

0
source

Source: https://habr.com/ru/post/897974/


All Articles