I have command line tool which runs tests. There is test runner class, which does some preparation before test execution and then runs the test and makes a report. Is it OK if my classes catch an exception and throw new custom exception to the upper layer, and then the upper layer will throw it to the upper layer too (until the View class, which will display/log the exception)?
class Program
{
static void Main(string[] args)
{
testRunner = new TestRunner();
try
{
testRunner.RunTest();
testRunner.GetReport();
}
catch (TestRunnerException ex)
{
Print(ex); // prints nicely two levels of exceptions for user
Log(ex); // writes to file all levels of exceptions
}
}
}
class TestRunner
{
void RunTest()
{
// run test here
}
TestReport GetTestReport()
{
try
{
return testReporter.GenerateReport();
}
catch (Exception ex)
{
throw new TestRunnerException("Failed to generate report.", ex);
}
}
}
class TestReporter
{
TestReport GenerateReport()
{
try
{
// create report here
}
catch (Exception ex)
{
throw new ReportException($"Test '{testName}' missing data.", ex)
}
}
}
It's not throwing custom exception from
catch
but catching all exceptions that is a bad practice; imagine:I suggest using specific exception(s) in the
catch
: