我有运行测试的命令行工具。有测试运行器类,它在测试执行之前做一些准备,然后运行测试并生成报告。如果我的类捕获异常并向上层抛出新的自定义异常,然后上层也将其抛出到上层(直到 View 类,它将显示/记录异常),是否可以?
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)
}
}
}