gpt4 book ai didi

c# - 异常句柄

转载 作者:太空宇宙 更新时间:2023-11-03 22:13:39 25 4
gpt4 key购买 nike

我需要知道这两个异常处理的女巫更好,为什么?或者其他一些做得更好的方法。

try{
if (String.IsNullOrEmpty(filePath))
{
throw new ArgumentNullException("The path is null or empty.", "filePath");
}

 try{
if (String.IsNullOrEmpty(filePath))
{
Console.WriteLine("The path is null or empty");
}

最佳答案

我会推荐第二种方法,但在这种情况下您不需要 try/catch。异常用于处理异常 情况。如果您可以使用 if 条件来防止这些情况,您应该更喜欢这个。如果您是性能优化狂热者,您可能已经知道与条件测试相比,抛出和捕获异常的成本更高。

关于c# - 异常句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5807742/

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