Wednesday, 7 August 2013

Rethowing an Exception in C#

Rethowing an Exception in C#

I'm using Ranorex a test automation tool which is based on c# and have a
question re: Exception Handling and rethrowing the exception. I'm are
fairly new to c# programming so please bear we me! Consider the following
code:
Child Class/Method..
try
{
do something;
'unhandled exception on line 150'
}
catch (exception)
{
throw;
}
parent Class/Method
try
{
childmethod();
}
catch (exception ex)
{
report.info("Info",ex.Stacktrace);
}
What i'd like to be able to do is report the line number that the
exception occured in, in the child class/method but in the Parent class
exception handler. This way in every child class/method we can just
rethrow (throw) the exception back up to the master class/method (i.e the
test case in Ranorex terms). In the parent exception handler we have other
things to do like reporting system details, closing the application and
failing the test. We only want to do this in one place, hence put in the
top level class. Using the code above however the stracktrace shows the
line number in the child exception handler that rethrows and also the line
number where the child method was called. It would also be useful if we
could just extract from the stacktrace something formatted like Class =
'class name' & Method = 'method name' & Line Num = 'line num' rather than
the whole stacktrace message.
We're using .net v4.0
Thanks for you help.

No comments:

Post a Comment