我有以下无法编译的代码,因为编译器对returnValue可能未定义感到犹豫:
export class AppComponent {
testForm = new FormGroup({
answer1: new FormControl(""),
answer2: new FormControl("")
});
public testString: string =
"<ul><li>When uranium was private T ReturnData<T>(string url, string method)
{
T returnValue;
try
{
returnValue = base.ReturnData<T>(url, method); // An http task
}
catch (AggregateException ae)
{
ae.Handle(inner =>
{
// Essentially rethrow any error immediately. Don't even handle just throw.
return false;
});
}
return returnValue;
}
to be radioactive</li><li> Marie Curie found that the element called {{2}} had the same property.</li></ul>";
ngOnInit() {
this.testString = this.testString.replace(
"throw
",
'<input type="text" formControlName="answer1" />'
);
this.testString = this.testString.replace(
"{{2}}",
'<input type="text" formControlName="answer2" />'
);
document.getElementById("myDiv").innerHTML = this.testString;
}
}
我正试图弄清为什么returnValue可以被初始化的原因。我使用Aggregate Exception Handler错误吗?我知道我只是返回false,基本上是重新抛出错误。为什么此代码无法编译?事情的哪一部分没有得到正确处理。
我注意到我可以在ae.Handle(...)
部分之后添加T returnValue;
,但是为什么这可以使事情正常?
请逗我一下,请稍等片刻,我可以改行
T returnValue = default;
进入
.Handle
它将编译。我专注于AggregateException的处理/理解,特别是使用{{1}}