gpt4 book ai didi

flutter - Dart throw 和重新 throw 有什么区别?

转载 作者:行者123 更新时间:2023-12-03 04:19:35 32 4
gpt4 key购买 nike

可能很明显,但是我仍然无法理解throwrethrow之间的区别,什么时候应该使用它们中的任何一个?

最佳答案

根据Effective Dart:

If you decide to rethrow an exception, prefer using the rethrow statement instead of throwing the same exception object using throw. rethrow preserves the original stack trace of the exception. throw on the other hand resets the stack trace to the last thrown position.


最大的不同是保留了原始堆栈跟踪。

他们提供了两个示例以显示预期的用法:
坏:
try {
somethingRisky();
} catch (e) {
if (!canHandle(e)) throw e;
handle(e);
}
好:
try {
somethingRisky();
} catch (e) {
if (!canHandle(e)) rethrow;
handle(e);
}

关于flutter - Dart throw 和重新 throw 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63422181/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com