Can you store a throw in a variable?

Asked

Viewed 88 times

3

I have the following code snippet:

try {
    throw new BadRequestHttpException("Error in Register of your Company");
} catch (BadRequestHttpException $e) {
    throw $e
}

I want to know if it is possible and it is good practice to use a variable to store this instance Exception (how is it in the code below)? Because I will need to use more than once and every time I launch a new throw not have to repeat everything again.

$badRequest = BadRequestHttpException("Error in Register of your Company");

try {
    throw new $badRequest;
} catch (BadRequestHttpException $e) {
    throw $e
}

1 answer

5


The throw can’t because he’s a statement and not an object, but the exception can yes, because it could not? It is an object like any other, as is a text or a numerical value.

Again I come to say that there is no such thing as good or bad practice, there is right or wrong for every concrete situation. What do you want to do? What problem do you expect to solve? For this you need to save the exception in a variable? Isn’t there a simpler way to do it? I notice that many people create variable without need of heap.

If you know that you will reuse the exception properly you can create it, but almost always this is not necessary, and it can be wrong if you do not understand very well what you are doing. When in doubt, don’t. When the exception is created it carries a lot of information about the execution and create in one place and release in another it may be that the information is outdated and give wrong instrumentation about what and where the error is occurring.

It is true that almost no one knows how to use exception correctly and maybe if the information is outdated does not make a difference to her, she does not even know that there is rich information about the error there, but it is a hypothesis and on top of something wrong, so I would not count on it. And if you don’t want to use the exception information you probably shouldn’t use it.

The problem with your code is throwing an exception inside a catch, Why would I do this? The reason to capture an exception is to solve the problem, if you can’t do this not capture the exception, capturing to re-launch doesn’t make any sense. This is wrong.

I doubt that most people who use exception had the curiosity to look at the documentation of Exception.

Without understanding deeply about exception I would limit its use to the minimum necessary (in PHP may be never use). Exception is one of the most complicated things that has in programming, full of subtle details and has other ways to get more or less the same.

  • Unfortunately the PHP documentation on the Exception class doesn’t help much either.

  • 2

    @bfavaretto but at least it shows that this exception has important information and all its derivatives will have also and has a first comment saying that this data is filled at the time of creation and not the release, so it can go bad if you need to. One of the reasons I tell people not to use exception because they are heavy (to carry or to create) and people don’t use any of this, so they should use something lighter.

  • Really "The reason to capture an exception is to solve the problem, if you can’t do this not capture the exception, capturing to re-launch doesn’t make any sense. This is wrong." (Maniero , 2019). The way I’m doing I’m just passing the error to the user, I have to send a message indicating friendly way to the user that their operation was not performed successfully. Thank you Maniero, your reply helped me to understand that it is not in all situations that I should use the try and the catch and that there are other ways to solve a problem. Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.