Phpinspectionsea: General Exception is thrown without a message

Created on 12 Nov 2017  路  5Comments  路  Source: kalessil/phpinspectionsea

I am thinking about an exception class that is autoexplain. For instance:

class UserNotFoundException extends RuntimeException {
}

Why I should declare a message when I thrown it?

fixed needs documenting

Most helpful comment

Oh, this answer is quite a guideline itself =) I'll add variants 1 and 3 to our documentation, thanks!

All 5 comments

You don't have to: overriding constructor with one having no parameters in the class (or your exceptions base class) will do the trick.

Can we have an option like "ignore if is not a base class"? Because I think that it does make sense only if I run it like "throw new RuntimeException" or if my child class have a constructor that receives a message as parameter.

I don't know if make sense I just create an empty constructor to make it works fine.

What do you think?

@hollodotme, do we have any guidelines for domain-specific exceptions (which we can use here)?

Well, guidelines - I don't think so.
I know of several variants of domain exceptions usage:

# Message is set internally in withUserId() method
throw (new UserNotFoundExeption())->withUserId($userId) 

# Message is passed to constructor internally in static withUserId() method
throw UserNotFoundException::withUserId($userId);

# Message is passed to parent::__construct() and created in __construct()
throw new UserNotFoundException($userId);

# Class name is used as message (property $message is not set)
throw new UserNotFoundException();

Personally, I usually use the first variant.

I would agree on an option to disable the inspection for Exceptions not in root namespace (\*Exception), disabled by default. This way the user can decide. But that's just my personal opinion.

Oh, this answer is quite a guideline itself =) I'll add variants 1 and 3 to our documentation, thanks!

Was this page helpful?
0 / 5 - 0 ratings