What is wrong with using a bare 'except'? [duplicate]
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?
python pyautogui except bare
marked as duplicate by jamesdlin, Christian Dean
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();
}
);
});
});
Mar 1 at 20:37
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.
add a comment |
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?
python pyautogui except bare
marked as duplicate by jamesdlin, Christian Dean
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();
}
);
});
});
Mar 1 at 20:37
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.
1
See also stackoverflow.com/q/4990718/20670
– Tim Pietzcker
Mar 1 at 16:25
1
Wikipedia has some good information on this--it's called error hiding.
– John Szakmeister
Mar 1 at 16:28
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
Mar 1 at 16:30
add a comment |
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?
python pyautogui except bare
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
python pyautogui except bare
edited Mar 1 at 20:49
Peter Mortensen
13.7k1986113
13.7k1986113
asked Mar 1 at 16:20
CaioRamaglioCaioRamaglio
411
411
marked as duplicate by jamesdlin, Christian Dean
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();
}
);
});
});
Mar 1 at 20:37
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
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();
}
);
});
});
Mar 1 at 20:37
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.
1
See also stackoverflow.com/q/4990718/20670
– Tim Pietzcker
Mar 1 at 16:25
1
Wikipedia has some good information on this--it's called error hiding.
– John Szakmeister
Mar 1 at 16:28
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
Mar 1 at 16:30
add a comment |
1
See also stackoverflow.com/q/4990718/20670
– Tim Pietzcker
Mar 1 at 16:25
1
Wikipedia has some good information on this--it's called error hiding.
– John Szakmeister
Mar 1 at 16:28
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
Mar 1 at 16:30
1
1
See also stackoverflow.com/q/4990718/20670
– Tim Pietzcker
Mar 1 at 16:25
See also stackoverflow.com/q/4990718/20670
– Tim Pietzcker
Mar 1 at 16:25
1
1
Wikipedia has some good information on this--it's called error hiding.
– John Szakmeister
Mar 1 at 16:28
Wikipedia has some good information on this--it's called error hiding.
– John Szakmeister
Mar 1 at 16:28
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
Mar 1 at 16:30
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
Mar 1 at 16:30
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
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.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
answered Mar 1 at 16:23
Adam SmithAdam Smith
34.6k63276
34.6k63276
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 1 at 16:24
Charlie MartinCharlie Martin
91.9k18165242
91.9k18165242
add a comment |
add a comment |
1
See also stackoverflow.com/q/4990718/20670
– Tim Pietzcker
Mar 1 at 16:25
1
Wikipedia has some good information on this--it's called error hiding.
– John Szakmeister
Mar 1 at 16:28
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
Mar 1 at 16:30