What is wrong with using bare except?About catching ANY exceptionWhat does ** (double star/asterisk) and *...

Dilemma of explaining to interviewer that he is the reason for declining second interview

Is casting an attack cantrip from a wand an "attack action made with a magic weapon"?

Is there a standard way to treat events with unknown times (missing time data)?

Why Normality assumption in linear regression

Citing paywalled articles accessed via illegal web sharing

Program that converts a number to a letter of the alphabet

How to deal with an incendiary email that was recalled

What's a good word to describe a public place that looks like it wouldn't be rough?

Slow moving projectiles from a hand-held weapon - how do they reach the target?

Are there neural networks with very few nodes that decently solve non-trivial problems?

How to prevent users from executing commands through browser URL

What is the most triangles you can make from a capital "H" and 3 straight lines?

Every character has a name - does this lead to too many named characters?

Explain the objections to these measures against human trafficking

difference between two quite-similar Terminal commands

What is the wife of a henpecked husband called?

Is there any differences between "Gucken" and "Schauen"?

Typing Amharic inside a math equation?

Checking for the existence of multiple directories

Would a National Army of mercenaries be a feasible idea?

Lick explanation

Using only 1s, make 29 with the minimum number of digits

Parsing a string of key-value pairs as a dictionary

What is a jet (unit) shown in Windows 10 calculator?



What is wrong with using bare except?


About catching ANY exceptionWhat does ** (double star/asterisk) and * (star/asterisk) do for parameters?How do I check whether a file exists without exceptions?What are metaclasses in Python?What is the difference between @staticmethod and @classmethod?What does the “yield” keyword do?What does if __name__ == “__main__”: do?What is __init__.py for?Manually raising (throwing) an exception in PythonCatch multiple exceptions in one line (except block)Creating a singleton in Python













8















I tried making a function to check if an image is displayed on screen using PyAutoGui and came up with this:



def check_image_on_screen(image):
try:
pyautogui.locateCenterOnScreen(image)
return True
except:
return False


And it works fine, but PyCharm tells me I shouldn't leave except bare. What is the problem with leaving it like this? Is there a more appropriate way of creating the same function?










share|improve this question









New contributor




CaioRamaglio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • See also stackoverflow.com/q/4990718/20670

    – Tim Pietzcker
    3 hours ago











  • Possible duplicate of About catching ANY exception

    – jamesdlin
    3 hours ago











  • Wikipedia has some good information on this--it's called error hiding.

    – John Szakmeister
    3 hours ago











  • I'm not sure this is a duplicate of that. This is asking "Why not bare except" while that one is asking "How do I bare except." A good answer for the latter probably answers the former, but that doth not a duplicate make.

    – Adam Smith
    3 hours ago
















8















I tried making a function to check if an image is displayed on screen using PyAutoGui and came up with this:



def check_image_on_screen(image):
try:
pyautogui.locateCenterOnScreen(image)
return True
except:
return False


And it works fine, but PyCharm tells me I shouldn't leave except bare. What is the problem with leaving it like this? Is there a more appropriate way of creating the same function?










share|improve this question









New contributor




CaioRamaglio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • See also stackoverflow.com/q/4990718/20670

    – Tim Pietzcker
    3 hours ago











  • Possible duplicate of About catching ANY exception

    – jamesdlin
    3 hours ago











  • Wikipedia has some good information on this--it's called error hiding.

    – John Szakmeister
    3 hours ago











  • I'm not sure this is a duplicate of that. This is asking "Why not bare except" while that one is asking "How do I bare except." A good answer for the latter probably answers the former, but that doth not a duplicate make.

    – Adam Smith
    3 hours ago














8












8








8








I tried making a function to check if an image is displayed on screen using PyAutoGui and came up with this:



def check_image_on_screen(image):
try:
pyautogui.locateCenterOnScreen(image)
return True
except:
return False


And it works fine, but PyCharm tells me I shouldn't leave except bare. What is the problem with leaving it like this? Is there a more appropriate way of creating the same function?










share|improve this question









New contributor




CaioRamaglio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I tried making a function to check if an image is displayed on screen using PyAutoGui and came up with this:



def check_image_on_screen(image):
try:
pyautogui.locateCenterOnScreen(image)
return True
except:
return False


And it works fine, but PyCharm tells me I shouldn't leave except bare. What is the problem with leaving it like this? Is there a more appropriate way of creating the same function?







python pyautogui except bare






share|improve this question









New contributor




CaioRamaglio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




CaioRamaglio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 3 hours ago









Tim Pietzcker

249k43376459




249k43376459






New contributor




CaioRamaglio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









CaioRamaglioCaioRamaglio

411




411




New contributor




CaioRamaglio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





CaioRamaglio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






CaioRamaglio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • See also stackoverflow.com/q/4990718/20670

    – Tim Pietzcker
    3 hours ago











  • Possible duplicate of About catching ANY exception

    – jamesdlin
    3 hours ago











  • Wikipedia has some good information on this--it's called error hiding.

    – John Szakmeister
    3 hours ago











  • I'm not sure this is a duplicate of that. This is asking "Why not bare except" while that one is asking "How do I bare except." A good answer for the latter probably answers the former, but that doth not a duplicate make.

    – Adam Smith
    3 hours ago



















  • See also stackoverflow.com/q/4990718/20670

    – Tim Pietzcker
    3 hours ago











  • Possible duplicate of About catching ANY exception

    – jamesdlin
    3 hours ago











  • Wikipedia has some good information on this--it's called error hiding.

    – John Szakmeister
    3 hours ago











  • I'm not sure this is a duplicate of that. This is asking "Why not bare except" while that one is asking "How do I bare except." A good answer for the latter probably answers the former, but that doth not a duplicate make.

    – Adam Smith
    3 hours ago

















See also stackoverflow.com/q/4990718/20670

– Tim Pietzcker
3 hours ago





See also stackoverflow.com/q/4990718/20670

– Tim Pietzcker
3 hours ago













Possible duplicate of About catching ANY exception

– jamesdlin
3 hours ago





Possible duplicate of About catching ANY exception

– jamesdlin
3 hours ago













Wikipedia has some good information on this--it's called error hiding.

– John Szakmeister
3 hours ago





Wikipedia has some good information on this--it's called error hiding.

– John Szakmeister
3 hours ago













I'm not sure this is a duplicate of that. This is asking "Why not bare except" while that one is asking "How do I bare except." A good answer for the latter probably answers the former, but that doth not a duplicate make.

– Adam Smith
3 hours ago





I'm not sure this is a duplicate of that. This is asking "Why not bare except" while that one is asking "How do I bare except." A good answer for the latter probably answers the former, but that doth not a duplicate make.

– Adam Smith
3 hours ago












2 Answers
2






active

oldest

votes


















12














Bare except will catch exceptions you almost certainly don't want to catch, including KeyboardInterrupt (the user hitting Ctrl+C) and Python-raised errors like SystemExit



If you don't have a specific exception you're expecting, at least except Exception, which is the base type for all "Regular" exceptions.





That being said: you use except blocks to recover from known failure states. An unknown failure state is usually irrecoverable, and it is proper behavior to fatally exit in those states, which is what the Python interpreter does naturally with an uncaught exception.



Catch everything you know how to handle, and let the rest propagate up the call stack to see if something else can handle it. In this case the error you're expecting (per the docs) is pyautogui.ImageNotFoundException






share|improve this answer































    1














    Basically, you're not taking advantage of the language to help you find problems. If you used except Exception as ex: you could do something like log the exception and know exactly what happened.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });






      CaioRamaglio is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54948548%2fwhat-is-wrong-with-using-bare-except%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      12














      Bare except will catch exceptions you almost certainly don't want to catch, including KeyboardInterrupt (the user hitting Ctrl+C) and Python-raised errors like SystemExit



      If you don't have a specific exception you're expecting, at least except Exception, which is the base type for all "Regular" exceptions.





      That being said: you use except blocks to recover from known failure states. An unknown failure state is usually irrecoverable, and it is proper behavior to fatally exit in those states, which is what the Python interpreter does naturally with an uncaught exception.



      Catch everything you know how to handle, and let the rest propagate up the call stack to see if something else can handle it. In this case the error you're expecting (per the docs) is pyautogui.ImageNotFoundException






      share|improve this answer




























        12














        Bare except will catch exceptions you almost certainly don't want to catch, including KeyboardInterrupt (the user hitting Ctrl+C) and Python-raised errors like SystemExit



        If you don't have a specific exception you're expecting, at least except Exception, which is the base type for all "Regular" exceptions.





        That being said: you use except blocks to recover from known failure states. An unknown failure state is usually irrecoverable, and it is proper behavior to fatally exit in those states, which is what the Python interpreter does naturally with an uncaught exception.



        Catch everything you know how to handle, and let the rest propagate up the call stack to see if something else can handle it. In this case the error you're expecting (per the docs) is pyautogui.ImageNotFoundException






        share|improve this answer


























          12












          12








          12







          Bare except will catch exceptions you almost certainly don't want to catch, including KeyboardInterrupt (the user hitting Ctrl+C) and Python-raised errors like SystemExit



          If you don't have a specific exception you're expecting, at least except Exception, which is the base type for all "Regular" exceptions.





          That being said: you use except blocks to recover from known failure states. An unknown failure state is usually irrecoverable, and it is proper behavior to fatally exit in those states, which is what the Python interpreter does naturally with an uncaught exception.



          Catch everything you know how to handle, and let the rest propagate up the call stack to see if something else can handle it. In this case the error you're expecting (per the docs) is pyautogui.ImageNotFoundException






          share|improve this answer













          Bare except will catch exceptions you almost certainly don't want to catch, including KeyboardInterrupt (the user hitting Ctrl+C) and Python-raised errors like SystemExit



          If you don't have a specific exception you're expecting, at least except Exception, which is the base type for all "Regular" exceptions.





          That being said: you use except blocks to recover from known failure states. An unknown failure state is usually irrecoverable, and it is proper behavior to fatally exit in those states, which is what the Python interpreter does naturally with an uncaught exception.



          Catch everything you know how to handle, and let the rest propagate up the call stack to see if something else can handle it. In this case the error you're expecting (per the docs) is pyautogui.ImageNotFoundException







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 3 hours ago









          Adam SmithAdam Smith

          34.5k53276




          34.5k53276

























              1














              Basically, you're not taking advantage of the language to help you find problems. If you used except Exception as ex: you could do something like log the exception and know exactly what happened.






              share|improve this answer




























                1














                Basically, you're not taking advantage of the language to help you find problems. If you used except Exception as ex: you could do something like log the exception and know exactly what happened.






                share|improve this answer


























                  1












                  1








                  1







                  Basically, you're not taking advantage of the language to help you find problems. If you used except Exception as ex: you could do something like log the exception and know exactly what happened.






                  share|improve this answer













                  Basically, you're not taking advantage of the language to help you find problems. If you used except Exception as ex: you could do something like log the exception and know exactly what happened.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 3 hours ago









                  Charlie MartinCharlie Martin

                  91.7k18165242




                  91.7k18165242






















                      CaioRamaglio is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      CaioRamaglio is a new contributor. Be nice, and check out our Code of Conduct.













                      CaioRamaglio is a new contributor. Be nice, and check out our Code of Conduct.












                      CaioRamaglio is a new contributor. Be nice, and check out our Code of Conduct.
















                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54948548%2fwhat-is-wrong-with-using-bare-except%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Benedict Cumberbatch Contingut Inicis Debut professional Premis Filmografia bàsica Premis i...

                      Monticle de plataforma Contingut Est de Nord Amèrica Interpretacions Altres cultures Vegeu...

                      Escacs Janus Enllaços externs Menú de navegacióEscacs JanusJanusschachBrainKing.comChessV