有没有一种方法可以装饰一个函数,记录一些日志,然后无条件抛出异常?像下面这样:
我有这样的代码:
void foo(out int x)
{
if( condition() ) { x = bar(); return; }
// notice that x is not yet set here, but compiler doesn't complain
throw new Exception( "missed something." );
}
如果我这样写,就会出现问题:
void foo(out int x)
{
if( condition() ) { x = bar(); return; }
// compiler complains about x not being set yet
MyMethodThatAlwaysThrowsAnException( "missed something." );
}
有什么建议吗?谢谢。