17
There have been cases where I needed to make use of methods that launched exception if any error occurred, but the exception would stop the execution of script, what would be something unwanted.
For example, I have a system where, when the student registers the request, an email is sent. The function that sends the email throws the exception. However, as the exception would stop the registration, I preferred to use a catch
empty. It was not as important to send the email as the request was registered.
So I did something like this:
try {
Mail::create('email.aluno')->send($callback);
} catch (\Exception $e) {
// Se não enviar o e-mail por conta de um erro
// continue o script
// Não vamos interromper esse momento tão importante
}
$usuario->fill($dados)->save();
One could say: just take the exception from where it is thrown. I don’t do this, because changing the third-party source code is not a good idea, because when updating the system, I would have to edit again, and again and again...
It’s a bad practice to use a catch
empty for nothing to happen when the exception occurs?
That would be an anti-standard?
There is a specific name for this type of operation?
There are risks in doing such an operation?
I think this answer shows that it is bad practice to leave the catch empty :) : http://answall.com/a/23787/27190
– gato
ugly as hell..... you can solve this with an if instead of using Try. is a system that does not treat exceptions, is already something bad, empty catch so...
– RBoschini
@Rboschini but with if, how would you "stop" the exception release?
– Wallace Maxters
exceptions are predictable, I would do the tests, identify the exceptions and deal with IF if necessary, never with an empty Try.
– RBoschini
I find the question complicated without specifying the language. For example, in Java, roughly speaking, everything is an exception. In PHP, almost (practically) everything is solved without exception. And, as the name says, exception should be treated as an exception (but in Java, make an exception to what I said). :)
– Bacco
A programming bug has to be predictable, if you do not understand why the exception is released or when it occurs, it is because you have not done the necessary tests to identify, you use Try to pass something you can treat the right way without bursting in the face of the user.
– RBoschini
@Rboschini maybe then I can take the specific exception and ignore it or else not ignore it. For example, the question of
catch
multiple.– Wallace Maxters
Multiple catch this OK, empty catch that no, in my opinion. ignore an exception I see how to prevent it from occurring, never ignore exception, give me an example of exceptions that you could ignore.
– RBoschini
If Try::catch is mandatory, would you like something "fault-Tolerant" is that? But I understand that in this case you only care about the empty catch. It is complicated because the catch may just be covering a problem elsewhere. In your case you could have a verifier to know if the email was actually sent, if not, could have another thread to try a new resend, meanwhile the registration continues.
– dougg0k
We can take the opposite view. If you feel like the empty catch is satisfactory, perhaps the specific exception to be discussed should not even be being cast. Not treating the exception can be a bad sign that you don’t understand the exception or it can be a bad sign of an erroneous exception being thrown. I find it difficult an empty catch being acceptable.
– Marco Aurélio Deleu
I’d say it’s bad practice.
– Jéf Bueno
If you’re making one
try{}catch(){}
, why don’t you treat? or at least generate a log, for future correction.– Guilherme Lautert
@Marcoaurélioderead the exception comes from the third-party library. I did not define it in this case.
– Wallace Maxters
@Guilhermelautert soon someone will say: "It is better to leave the profession",rsrsrsrs.
– Wallace Maxters
@Wallace Maxters excuses curiosity, but what makes this exception lib and what exception does it burst? out of curiosity
– RBoschini
@Wallacemaxters I know this as Pattern silenciator, it has good use to hide possible failures .... but it is great for Voce to take hours to figure out the problems.... yes very bad use
– Otto
@Rboschini is as follows. In this part of the system, I can send an email to the student when their photos are approved. The problem is that I cannot fail to approve these photos just because the email was not sent, so I used the empty catch. Today, of course, I would have other ways to solve this, since I now have time to think of something better.But the problem is that in this system the approval of the images and the sending of the email occur together. As I was giving dick in the email once while (Brazilian servers!), I had to put this
catch
. The exception comes from the third-party code.– Wallace Maxters