代码之家  ›  专栏  ›  技术社区  ›  Oleg Sh

类库的异常属性

  •  0
  • Oleg Sh  · 技术社区  · 7 年前

    我的服务类有许多方法,这些方法调用其他服务,而此服务有指定的异常。我想在方法捕获这个指定的异常时抛出我的异常。

    try
    {
        // call other service
    }
    catch(ServiceXxxException serviceEx)
    {
        throw new MyException(...);
    }
    

    但我有很多这样的方法,我不想增加代码。是否可以创建异常属性,如 ExceptionFilterAttribute 对于ASP.NET MVC/Core?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Mohammad Ghanem    7 年前

    CustomExceptionFilter]  
    public class HomeController:Controller 
    {  
       //......  
    }  
    
    
    //Over the Action  
    [CustomExceptionFilter]  
    public ActionResult Index() 
    {  
       //.......  
    }  
    

    请遵循本文:

    https://www.c-sharpcorner.com/UploadFile/0ef46a/exception-filters-in-mvc/

    推荐文章