sách gpt4 ăn đã đi

c# - ApplicationException 或创建自定义异常?

In lại 作者:太空狗 更新时间:2023-10-29 18:16:18 25 4
mua khóa gpt4 giày nike

在我的文件存储库中,当调用 InsertFile() 方法时,我将抛出以下异常:

  • 超过上传文件大小限制时
  • 超出存储容量时

目前我只是抛出一个带有相关消息的ApplicationException:

public void InsertFile(HttpPostedFile uploadedFile)
{
if (uploadedFile.ContentLength > FileSizeLimit)
{
throw new ApplicationException("File size limit exceeded.");
}

if (uploadedFile.ContentLength + FileStorageUsage > FileStorageCapacity)
{
throw new ApplicationException("File storage capacity exceeded.");
}

// ...
}

câu hỏi:

我应该在这里使用更好的异常类吗?

或者我应该通过从 ApplicationException 派生来创建我自己的自定义异常吗?

câu trả lời hay nhất

也许阅读 tài liệu :

If you are designing an application that needs to create its own exceptions, you are advised to derive custom exceptions from the Ngoại lệ class. It was originally thought that custom exceptions should derive from the ApplicationException class; however in practice this has not been found to add significant value.

至于是否有更好的异常抛出 - 有些人可能会考虑抛出 ArgumentOutOfRangeException如果您不想定义自己的异常。

关于c# - ApplicationException 或创建自定义异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16603065/

25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress