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
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
New contributor
add a comment |
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
New contributor
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
add a comment |
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
New contributor
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
python pyautogui except bare
New contributor
New contributor
edited 3 hours ago
Tim Pietzcker
249k43376459
249k43376459
New contributor
asked 3 hours ago
CaioRamaglioCaioRamaglio
411
411
New contributor
New contributor
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
add a comment |
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
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 |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 3 hours ago
Adam SmithAdam Smith
34.5k53276
34.5k53276
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 3 hours ago
Charlie MartinCharlie Martin
91.7k18165242
91.7k18165242
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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