Python Error Handling Goto
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta
Error Handling Python 3
Discuss the workings and policies of this site About Us Learn error handling c++ more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us error handling perl Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like
Error Handling Ruby
you, helping each other. Join them; it only takes a minute: Sign up On Error Resume Next in Python up vote 4 down vote favorite 1 Snippet 1 do_magic() # Throws exception, doesn't execute do_foo and do_bar do_foo() do_bar() Snippet 2 try: do_magic() # Doesn't throw exception, doesn't execute do_foo and do_bar do_foo() do_bar() except: pass Snippet
Error Handling Php
3 try: do_magic(); except: pass try: do_foo() ; except: pass try: do_bar() ; except: pass Is there a way to write code snippet 3 elegantly? if do_magic() fails or not, do_foo() and do_bar() should be executed. if do_foo() fails or not, do_bar() should be executed. In Basic/Visual Basic/VBS, there's a statement called On Error Resume Next which does this. python exception error-handling share|improve this question edited Sep 26 '14 at 12:33 asked Sep 26 '14 at 12:18 Elmo 2,279844103 4 And On Error Resume Next is a crime against control flow! –jonrsharpe Sep 26 '14 at 12:26 Can you decorate those function like here? stackoverflow.com/a/9386604/1532474 –Dominik Neise Sep 26 '14 at 12:28 You would be interested in Exhibit A and Exhibit B. –Veedrac Sep 27 '14 at 7:03 add a comment| 4 Answers 4 active oldest votes up vote 9 down vote accepted In Python 3.4 onwards, you can use contextlib.suppress: from contextlib import suppress with suppress(Exception): # or, better, a more specific err
[ date ] [ thread ] [ subject ] [ author ] This is *exactly* what Python does right now! class ToFailError(Exception): pass try: do_some_stuff() if python catch all errors (some_error): raise ToFailError do_other_stuff() try: thing_that_might_fail() except SomeOtherError: handle_it() except ToFailError: here_we_are() On
Python Try Else
Wed, Mar 5, 2014 at 7:54 PM, Sturla Molden
tour help Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta http://programmers.stackexchange.com/questions/139171/defensive-programming-vs-exception-handling Discuss the workings and policies of this site About Us Learn http://programmers.stackexchange.com/questions/189222/are-exceptions-as-control-flow-considered-a-serious-antipattern-if-so-why more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Software Engineering Questions Tags Users Badges Unanswered Ask Question _ Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems error handling development life cycle who care about creating, delivering, and maintaining software responsibly. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Defensive Programming vs Exception Handling? up vote 65 down vote favorite 29 I'm working python error handling through the book "Head First Python" (it's my language to learn this year) and I got to a section where they argue about two code techniques: Defensive coding vs Exception handling. Here is a sample of the Python code: # Defensive Coding for eachLine in open("../../data/sketch.txt"): if eachLine.find(":") != -1: (role, lineSpoken) = eachLine.split(":",1) print("role=%(role)s lineSpoken=%(lineSpoken)s" % locals()) # Exception handling for eachLine in open("../../data/sketch.txt"): try: (role, lineSpoken) = eachLine.split(":",1) print("role=%(role)s lineSpoken=%(lineSpoken)s" % locals()) except: pass The first example deals directly with a problem in the .split function. The second one just lets the exception handler deal with it (and ignores the problem). They argue in the book to use exception handling instead of defensive coding. The argument is that the exception code will catch all errors, where defensive coding will only catch the things you think about (and you miss the corner cases). I have been taught to code defensively, so my intial instinct was to code defensively, but their idea is interesting. I had never thought of using the exceptio
tour help Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Software Engineering Questions Tags Users Badges Unanswered Ask Question _ Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle who care about creating, delivering, and maintaining software responsibly. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Are exceptions as control flow considered a serious antipattern? If so, Why? up vote 47 down vote favorite 16 Back in the late 90's I worked quite a bit with a code base that used exceptions as flow control. It implemented a finite state machine to drive telephony applications. Lately I am reminded of those days because I've been doing MVC web apps. They both have Controllers that decide where to go next and supply the data to the destination logic. User actions from the domain of an old-school telephone, like DTMF tones, became parameters to action methods, but instead of returning something like a ViewResult, they threw a StateTransitionException. I think the main difference was that action methods were void functions. I don't remember all the things I did with this fact but I've been hesitant to even go down the road of remembering much because since that job, like 15 years ago, I never saw this in production code at any other job. I assumed this was a sign that it was a so-called anti-pattern. Is this the case, and if so, why? Upd