user217648
user217648

Reputation: 3466

How to find out where in try-block the exception occured

I have a try-catch block. In the catch block, I want to logg information where in the try block the exception occured (i.e linke numnber). thanks

Upvotes: 1

Views: 88

Answers (2)

Anthony Horne
Anthony Horne

Reputation: 2522

You can try and look at the stack object to see where it got. It generally gives you a line number (with lots of other "bits")

I use it to generically, get the calling method for generic error handling:

void DoThings(stacktrace se)
{
se.GetFrame(0).GetMethod().ReflectedType.Name 
}

gets me the calling method info.

Upvotes: 1

sumgeek
sumgeek

Reputation: 76

You should apply try-catch block to the code where you are not sure that it will work or not (i.e., You are doubted about the code )

When a try-catch block is applied on multiple lines, and want to know that on what line the exception has occurred, I think you need to surround each line by try-catch block.

Otherwise Exception message will reveal the error occurred during execution.

Upvotes: 2

Related Questions