How to catch the original (inner) exception in C#?

You need c# 6 / visual studio 2015 in order to do this using a predicate:

catch (ArgumentException e) when (e.ParamName == “…”)
{
}

Official C# Try/Catch Documentation


I hate to have to tell you this, but you cannot catch an inner exception.

What you can do is inspect one.

I suggest you catch your high-level exception (I believe it was LockerException) and inspect the InnerException property of that exception. Check the type, and if it's not a SqlException, check the InnerException of that exception. Walk each one until you find a SqlException type, then get the data you need.

That said, I agree with dasblinkenlight that you should consider -- if possible -- a heavy refactor of your exception framework.

Tags:

C#

Exception