What is wrong with using a bare 'except'? [duplicate]About catching ANY exceptionWhat does ** (double...

Why is working on the same position for more than 15 years not a red flag?

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

Is a debit card dangerous for an account with low balance and no overdraft protection?

Groups acting on trees

Can I write a book of my D&D game?

Why does a metal block make a shrill sound but not a wooden block upon hammering?

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

How do I say "Brexit" in Latin?

Check if the digits in the number are in increasing sequence in python

Is there some relative to Dutch word "kijken" in German?

Can we use the stored gravitational potential energy of a building to produce power?

How to tag distinct options/entities without giving any an implicit priority or suggested order?

Notes in a lick that don't fit in the scale associated with the chord

Checking for the existence of multiple directories

If I delete my router's history can my ISP still provide it to my parents?

Can a hotel cancel a confirmed reservation?

Avoiding morning and evening handshakes

Word or phrase for showing great skill at something without formal training in it

Why avoid shared user accounts?

How to avoid being sexist when trying to employ someone to function in a very sexist environment?

Why would the Pakistan airspace closure cancel flights not headed to Pakistan itself?

How do you funnel food off a cutting board?

What kind of hardware implements Fourier transform?

Explain the objections to these measures against human trafficking



What is wrong with using a bare 'except'? [duplicate]


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
















This question already has an answer here:




  • About catching ANY exception

    8 answers




I tried making a function to check if an image is displayed on the 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.











marked as duplicate by jamesdlin, Christian Dean python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
10 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















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

    – Tim Pietzcker
    15 hours ago











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

    – John Szakmeister
    15 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
    15 hours ago
















8
















This question already has an answer here:




  • About catching ANY exception

    8 answers




I tried making a function to check if an image is displayed on the 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.











marked as duplicate by jamesdlin, Christian Dean python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
10 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















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

    – Tim Pietzcker
    15 hours ago











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

    – John Szakmeister
    15 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
    15 hours ago














8












8








8









This question already has an answer here:




  • About catching ANY exception

    8 answers




I tried making a function to check if an image is displayed on the 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.













This question already has an answer here:




  • About catching ANY exception

    8 answers




I tried making a function to check if an image is displayed on the 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?





This question already has an answer here:




  • About catching ANY exception

    8 answers








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 10 hours ago









Peter Mortensen

13.7k1986112




13.7k1986112






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 15 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.




marked as duplicate by jamesdlin, Christian Dean python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
10 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by jamesdlin, Christian Dean python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
10 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















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

    – Tim Pietzcker
    15 hours ago











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

    – John Szakmeister
    15 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
    15 hours ago



















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

    – Tim Pietzcker
    15 hours ago











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

    – John Szakmeister
    15 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
    15 hours ago

















See also stackoverflow.com/q/4990718/20670

– Tim Pietzcker
15 hours ago





See also stackoverflow.com/q/4990718/20670

– Tim Pietzcker
15 hours ago













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

– John Szakmeister
15 hours ago





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

– John Szakmeister
15 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
15 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
15 hours ago












2 Answers
2






active

oldest

votes


















14














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






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      14














      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




























        14














        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


























          14












          14








          14







          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 15 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 15 hours ago









                  Charlie MartinCharlie Martin

                  91.7k18165242




                  91.7k18165242















                      Popular posts from this blog

                      Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

                      Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

                      Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...