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

What is better: yes / no radio, or simple checkbox?

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

How should I handle players who ignore the session zero agreement?

Is a debit card dangerous in my situation?

What was the earliest start time of a Catholic mass before 1957?

What is the in-universe cost of a TIE fighter?

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

"Free" Hopf algebra

Why is "points exist" not an axiom in geometry?

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

cron not executing python3

Why zero tolerance on nudity in space?

How do you funnel food off a cutting board?

Find x angle in triangle

Disable the ">" operator in Rstudio linux terminal

Strange Sign on Lab Door

Why Normality assumption in linear regression

Why do members of Congress in committee hearings ask witnesses the same question multiple times?

How to explain planetary rings pulsating?

We are very unlucky in my court

A starship is travelling at 0.9c and collides with a small rock. Will it leave a clean hole through, or will more happen?

difference between two quite-similar Terminal commands

Why does String.replaceAll() work differently in Java 8 from Java 9?

How do I say "Brexit" in Latin?



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


















11














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









      11














      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




























        11














        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


























          11












          11








          11







          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

                      “%fieldName is a required field.”, in Magento2 REST API Call for GET Method Type The Next...

                      How to change City field to a dropdown in Checkout step Magento 2Magento 2 : How to change UI field(s)...

                      夢乃愛華...