Can using Try catch nested in php result in problems?

Asked

Viewed 23 times

0

I want to run several searches in the database using the structure Try catch of php, I wonder if there can be any error if I use them nested.

Example:

try {
    
    # Código para pegar todos os usúarios
    
    try {
        
        # Código para pegar todas as postagens

   } catch (...) {...}


} catch (...) {...}
  • 1

    but why do you want to do this? Try/catch is to treat a code block, you can put too much code in Try, because you would need another?

1 answer

0

Error nesting structures "Try-catch", nay. The problem, as commented by @Ricardopunctual in the question is precisely needing more than a "Try-catch", not by syntax but by the structure of the code itself. Makes the same redundant, makes its readability difficult etc.

The goal of the structure is to capture exceptions (or errors) triggered by calls within the block try. If I understand your use case correctly:

  • You can redeem your "users" and "posts" in the same "Try-catch" structure within it try, if any of the rescues fires an exception or error will fall into the block catch;

  • If necessary treat the errors separately, you can separate rescues into functions and these, in turn, have the "Try-catch" structures implemented in the way that best suits each one. So you can do your rescues and handle the bugs separately if you think it suits your case.

I hope I’ve helped in some way.

Browser other questions tagged

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