How to use Java-style throws keyword in C#?

前端 未结 10 2166
说谎
说谎 2020-11-27 04:39

In Java, the throws keyword allows for a method to declare that it will not handle an exception on its own, but rather throw it to the calling method.

Is there a sim

10条回答
  •  难免孤独
    2020-11-27 05:18

    Actually not having checked exceptions in C# can be considered a good or bad thing.

    I myself consider it to be a good solution since checked exceptions provide you with the following problems:

    1. Technical Exceptions leaking to the business/domain layer because you cannot handle them properly on the low level.
    2. They belong to the method signature which doesn't always play nice with API design.

    Because of that in most bigger applications you will see the following pattern often when checked Exceptions occur:

    try {
        // Some Code
    } catch(SomeException ex){
        throw new RuntimeException(ex);
    }
    

    Which essentially means emulating the way C#/.NET handles all Exceptions.

提交回复
热议问题