简体   繁体   English

C#我应该在这里提出什么例外?

[英]C# What exception should I raise here?

[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();

        // ...
    }
}

There are quite a few options available but none seem to be particularly designed for cases like this. 有很多可用的选项,但似乎没有一个是专门针对此类情况设计的。

ie: 即:

  • UnauthorizedAccessException: I/O UnauthorizedAccessException:I / O
  • AccessViolationException: Memory stuff AccessViolationException:内存中的东西
  • SecurityAccessDeniedException : Represents the security exception that is thrown when a security authorization request fails. SecurityAccessDeniedException :表示安全授权请求失败时引发的安全异常。

etc. 等等

Should I create my own type of exception for this? 我是否应该为此创建自己的异常类型? What exception should be raised when membership users don't have enough priviledges to invoke a method? 当成员资格用户没有足够的特权来调用方法时,应该引发什么异常?

AccessViolationException is a bad idea. AccessViolationException是一个坏主意。 It should only be thrown by the runtime itself. 它只能由运行时本身抛出。 And it indicates a very severe failure(Your memory got corrupted) where the only appropriate response is terminating the process. 这表明一个非常严重的故障(您的内存已损坏),其中唯一适当的响应是终止该过程。 An invalid web request is certainly not equivalent to corrupted memory. 无效的Web请求当然并不等同于损坏的内存。

UnauthorizedAccessException sounds like a decent choice. UnauthorizedAccessException听起来像是一个不错的选择。 You might want to derive your own class from it to create a more specific exception. 您可能想要从中派生自己的类以创建更具体的异常。

UnauthorizedAccessException seems adequate; UnauthorizedAccessException似乎足够; it's not reserved for IO operations. 它不是为IO操作保留的。 But if you don't like it, just create your own. 但是,如果您不喜欢它,只需创建自己的即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM