I want to ask is there in C# analogue for C++ "noexcept" attribute for methods which does not generate any exceptions. Something like that:
public int SomeMethod() noexcept
{
throw new ArgumentNullException (); //Compile error: Method with 'noexcept'
attribute can't generate exceptions.
}
And when I want to call this method in my code I need not to think about try/catch block for this method.
So far as I know, there is nothing like that in C# (though I'm not 100% on that). With regards to it's use; when writing clean code you should only handle exceptions as and where you expect them to occur. This allows for the flow and control of your program to be much more manageable.
If you are unaware of an exception that could occur during a method, or an exception occurs that you did not expect, then you need to determine whether that exception is something that is considered critical (in that it should cause the application to fail); or whether it should be specifically handled within the code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With