Search code examples
c#exceptionstackstack-frame

Determining which code line threw the exception


In dotNet a line throws an exception and is caught, how can I figure out which line in which file threw the exception? Seems relatively straightforward, but I can't figure it out...


Solution

  • You can only do it if you have debug symbols available.

    catch(Exception ex) {
        // check the ex.StackTrace property
    }
    

    If you want to debug such a situation in VS, you'd better just check Thrown checkbox for Common Language Runtime Exceptions in Exceptions dialog located in Debug menu. The debugger will break as soon as the exception is thrown, even if it's in a try block.