T
Each case is a case, but in general nesting try is not the most suitable. This probably indicates that you are capturing more exceptions than you should.Anyway, normally you just want to give an exception treatment. try catch is not a normal flow control, so it rarely makes sense to have them nested. The release of an exception is a long jump, that is, he goes to a distant place, possibly unknown. So it doesn't matter so much where the catch, it can, and it is very common, that it is not in the current function. This is normal.The important thing when an exception is released is that it is captured by some catch somewhere in your application code, anywhere that is suitable to capture and do something useful to try to recover from the error. Don't worry about the flow, it will already be "disvirtuated" of your normal after the throw.This capture can be unique throughout the system, only for log the error and present in a "good" way to the user. Do not abuse the capture of errors. See more about this in https://pt.stackoverflow.com/a/30168/101 . It is important to note that using a catch doesn't do miracles, he doesn't solve problems by himself. On the contrary, your exaggeration makes you hide mistakes.Then in his example the "most correct" would be:try {
$bancoDeDados = new Bd();
$bancoDeDados->createCreateEntidade($valor1,$valor2);
$this->geraPdf($valor1,$valor2);
}
catch (PDOException $e) {
//faz alguma coisa
}
catch(ExceptionSeiLa $e) {
//faz alguma coisa
}
https://github.com/bigown/SOpt/blob/master/PHP/Syntax/NestedTryCatch.php If you give a problem PDOExcption when to call Bd() or createCreateEntidade he will divert to the first catch. If it occurs ExceptionSeiLa, probably generated by the method geraPdf, the deviation will be for the second catch.One catch does not need and in general should not be associated with a try exclusive. You should treat all the exceptions you need to treat at this time in a dull way, and not once in a nested way.Of course. can there is some situation that nesting is useful. But it's unusual.If an exception is generated before performing geraPdf(), probably by Bd() or createCreateEntidade, certainly it will not be executed, the exception generates a deviation before reaching it and does what you want. Depending on what you want, maybe even the second catch be unnecessary.But avoid abusing the capture of exceptions that you can't do anything useful. I understand that you have just made a simple example, but if you capture an exception and don't do anything with it, something is wrong.Personally if I can avoid the use of exception, I do. If I can check with if If there's something wrong, I'll do it. I only use exception if:the algorithm with if get more confused than with try catchcan generate a https://pt.stackoverflow.com/q/159342/101 or considerable loss of performancethe API obliges to do this (there is no other way available to "touch" the error).This last case is very common in Java. Language culture and environment encourage the use of exceptions.I don't have much experience with the new PHP APIs, but what I've always used from PHP, exceptions are rarely needed. It always has a way to resolve the issue without its use. But PHP started without exceptions if it worked well without them. Of course its addition brought new perspectives but also brought much abuse.You can still fall in the first two cases. In the first can be more advantageous to prefer the exception and in the second can be required to have the correct solution and with proper performance.