Does a method that can throw an exception have a β€œthrow” in its name?

I am writing a method that ensures that the caller can execute it. When the user is not authorized, I should throw an exception (see the code below). What should be the name of this method?

public void Compute() {
    this.CheckIfCallerAuthorizedOrThrow(); // Is it a good name? Better idea?

    // ...
}

I want to make sure that someone who reads the code understands that the method thread can be broken into an exception here. Is it good or bad practice in this case to have a method name with a "cast" in it?

+3
source share
5 answers

, , . . - : " , ...". , , , .

XML. XML . , , , .

"throw" , , . :

private void doSomethingOrThrow(object args)
{
    if (args == null)
        throw new ArgumentNullException();
    doSomething();
}

, , . "throw" , . , , , (, , , ...)

+7

, Java . , XML-. :

/// <summary>
/// Checks to see if the caller is authorized to access the resource.
/// </summary>
/// <exception cref="ArgumentException">
/// Throws invalid argument exception when an invalid parameter is supplied.
/// </exception>
CheckIfCallerAuthorized(sring myArgument)
{
  throw new ArgumentException("Invalid argument was provided for myArgument.");
}
+4

, OrThrow/Throws , .

assert/make/verify "check" ( true/false) . , , "EnsureCallerAuthorized" .

+2
source

This is obviously a personal taste issue, but the more common idiom is in the "assert" family, sort of assertCallerIsAuthorized().

+1
source

From what I read, the fact that this method cannot be executed is not exceptional. This can often happen during program execution. If this assumption is correct, I would advise actually using an exception to control program flow.

0
source

All Articles