![]() Anything that's more readable is definitely going to be better in the end, especially when you have to go back and touch that code in 6 months. That way you get some reuse out of the "CallDB" function and you keep the retry code in a different function.įor very small functions, you could argue that a label and goto do not make the code less readable, but there's almost always a *more* readable way to write the code that would not use the label and goto. But if you cant update the classes, in PHP 7, you can catch. Then, you can catch that exception instance. First, if you are able to modify these exceptions, you should make them extend a common base class or implement a common interface. The call to InsertWithUniqueName could call CallDB and, if it violated the unique constraint, popup the message and call CallDB again. And if you find yourself catching multiple exceptions, there are two solutions. something like AppControllersConnectionException), which will never exist. Suppose you had two functions instead of one: InsertWithUniqueName and CallDB. Otherwise, youre actually trying to catch something in the current namespace named ConnectionException (i.e. The rest of the code in the class is kind of a mess as Im waiting for an answer about the re-trying. In particular, the use of goto is generally frowned on, mostly because it generally makes code less readable. How can I make it go back to the try after a catch boolean condition is declared properly, no compilation errors or anything. John_0025, for what you want I would suggest a little refactoring to make your code more readable. But maybe is *should* make my own language! However, it needs to be able to automatically retry the request on. ![]() If you throw exceptions to give validation notices you need to catch those in a different layer (and you're probably doing it wrong if you use exceptions for that).I was just using pseudo-code, to show the general feel for a "3 try, retry" in a function. Ive written some code in Laravel 5.2 to retrieve results from an unrelible API source. If method A retries ten times before throwing, and method B calls method A and it retries ten times before throwing, and method C calls method B and it retries ten times before throwing, then we actually try operation A a thousand times. Please be advised that construct () is passed an array of unsanitized post values that have been organized into an array after the. begingroup My standard caution for this pattern: it does not handle composition well. I am wondering if I'm on the right track, and what ways, if any, I can improve my code. The user must not be interested what went wrong other than it just didn't work out and your program crashed. I am using try / catch syntax in combination with a database transaction to (hopefully) prevent partial registrations. Otherwise if you state some return type, the IDE is warning you that you are not returning it. Foti at 15:28 skler no but then they should be set to return void. If you have access to your php.ini file, you can go in there and add displayerrors on and that should allow you to see what errors your PHP code is throwing. ![]() ![]() You can then write code to return the internal server error because an exception signals an exceptional case so it normally is an 500 internal server error. 1 ide are stupid :) Not all methods must return something. As for the 500 error, thats probably appearing because PHP errors are turned off on your server. They play a crucial role in exception handling. $controllerService->handleControllerException($e) PHP try-catch is the basic block with the feature of exception handling, that contains the code to handle exceptions. If I were you I would use an ordinary for-loop with an iterator. Should I use try/catch or die or something else. It is designed to give you the next item in the iterator. Load 7 more related questions Show fewer related questions Sorted by: Reset to default Know someone who can answer Share a link to this question via email, Twitter, or. I am curious of the fastest and safest way to (catch an error) and continue if the system is not able to do the insert into MySQL. ![]() This is best solved with a frontend controller that is able to catch all uncatched exceptions: execute($request) I dont think that you can with a foreach. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |