【发布时间】:2020-10-13 20:46:43
【问题描述】:
所以我试图错误处理我的代码,到目前为止,这就是我所拥有的:
date = GetDate();
if(date.throws_exception())
{
// would it be possible to make a condition for where you can say if date throws exception?
}
string GetDate()
{
try
{
.
.
.
return date;
}
catch(Exception ex)
{
throw new Exception();
}
}
我想知道 if 条件是否有可能,你能说:
if(date throws exception)
【问题讨论】:
-
您是否有理由不想在 catch-block 中做这些事情?
-
是的,以间接方式。不是
GetDate返回一个字符串,而是返回一个元组(bool exceptionThrown, string value),其中的布尔值指示是否抛出了异常。你可以做if(date.exceptionThrown) -
有什么原因不能使用内置的
DateTime.TryParse方法吗? -
您是否正在尝试制定错误处理策略?如果是这样,您可能希望冒泡您的错误,而不是创建太多
result对象来检查错误。一个可能有助于您的错误处理策略的链接,如果这是您对这个问题的意图...stackoverflow.com/questions/14973642/…
标签: c# if-statement exception try-catch throw