Catch multiple exceptions at once?

前端 未结 27 1891
夕颜
夕颜 2020-11-22 11:31

It is discouraged to simply catch System.Exception. Instead, only the "known" exceptions should be caught.

Now, this sometimes leads to unnecce

27条回答
  •  粉色の甜心
    2020-11-22 12:20

    Cautioned and Warned: Yet another kind, functional style.

    What is in the link doesn't answer your question directly, but it's trivial to extend it to look like:

    static void Main() 
    { 
        Action body = () => { ...your code... };
    
        body.Catch() 
            .Catch() 
            .Catch(ex => { ...handler... })(); 
    }
    

    (Basically provide another empty Catch overload which returns itself)

    The bigger question to this is why. I do not think the cost outweighs the gain here :)

提交回复
热议问题