gpt4 book ai didi

C# 我应该在这里引发什么异常?

转载 作者:太空狗 更新时间:2023-10-29 23:17:49 25 4
gpt4 key购买 nike

[WebService(Namespace = "http://service.site.com/service/news")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[ScriptService]
public class NewsService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod]
public void DoPost(string title, string markdown, int categoryId)
{
if (!MembershipHelper.IsAtLeast(RoleName.Administrator))
throw new AuthenticationException();

// ...
}
}

有很多选项可用,但似乎没有一个是专门为这种情况设计的。

即:

  • 未授权访问异常:I/O
  • AccessViolationException:内存问题
  • SecurityAccessDeniedException : 表示安全授权请求失败时抛出的安全异常。

等等

我应该为此创建自己的异常类型吗?当成员用户没有足够的权限来调用方法时应该引发什么异常?

最佳答案

AccessViolationException 是个坏主意。它应该只由运行时本身抛出。它表示非常严重的故障(您的内存已损坏),唯一适当的响应是终止进程。无效的 Web 请求当然不等同于损坏的内存。

UnauthorizedAccessException 听起来是个不错的选择。您可能希望从中派生自己的类以创建更具体的异常。

关于C# 我应该在这里引发什么异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7373079/

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