In a .net Exception how to get a stacktrace with argument values

前端 未结 11 1417
南方客
南方客 2020-12-01 01:58

I am trying to add an unhandled exception handler in .net (c#) that should be as helpfull for the \'user\' as possible. The end users are mostly programers so they just need

11条回答
  •  遥遥无期
    2020-12-01 02:44

    I don't know a way to obtain the values of the arguments of each function in the call stack trace, but one solution would be to catch the specific exception (KeyNotFoundException in your example) and re-throw it in a new Exception. That allows you associate any additional information you wish

    For example:

    Dim sKey as String = "some-key"
    Dim sValue as String = String.Empty
    
    Try
       sValue = Dictionary(sKey)
    Catch KeyEx As KeyNotFoundException
       Throw New KeyNotFoundException("Class.Function() - Couldn't find [" & sKey & "]", KeyEx)
    End Try
    

    I appreciate your statement about localising the error strings, but if your audience are 'mostly programmers' then convention already dictates a comprehension of English to some degree (rightly or wrongly - but that's another debate!)

提交回复
热议问题