Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Noexcept equivilent in C#

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.

like image 958
Vasya Avatar asked Oct 23 '22 20:10

Vasya


1 Answers

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.

like image 164
Samuel Slade Avatar answered Nov 15 '22 05:11

Samuel Slade