Posts: 4,646
Threads: 1,493
Joined: Sep 2016
i'm trying to handle reading a cache file using try/except. if the reading fails for any reason, the except clause goes out to the internet and tries to get the data, which is slow. but i want to include one test in the try clause which is an ordinary test ... is the cache over 32 days old. if it is too old i want the exception code to run so that it gets the live data, again. so, i'm thinking that the simple way to trip into the except clause is to just raise an exception it handles, like ValueError. does that make sense? will it work and be handled like any other exception in its list?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 158
Threads: 27
Joined: Jan 2019
Dec-13-2019, 05:24 AM
(This post was last modified: Dec-13-2019, 05:24 AM by Clunk_Head.)
Quote:can i raise an exception in a try clause?
Yes, this is often the reason for using a try / except block. Simply raise the exception, but be sure to use the correct exception type. In you case a datum that is too old would be a ValueError as you suggested.
I use a try with a half-dozen except blocks to help the user under their mistakes in my calculator application.
Now here's the important question. Have you tried it?
Try it and post your results here if you have errors.
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
i don't understand what it is you are suggesting that i try. can you show an example?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 158
Threads: 27
Joined: Jan 2019
(Dec-13-2019, 11:10 PM)Skaperen Wrote: i don't understand what it is you are suggesting that i try. can you show an example?
What have you tried?
You can raise an exception with the keyword raise. Then you have to catch that specific exception and take action. This is not outside of the normal use of the try/except.
There is an example here.
Posts: 4,786
Threads: 76
Joined: Jan 2018
For better code, I would raise a specific exception such as CacheTooOldError and catch it. The same except clause can catch several types of exceptions.
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
i have tried nothing, yet. unless i can come up with a good test the covers all possible cases, i don't bother to waste my time trying it. for this, my concern is whether a direct raise might, for some exceptions, be handled different because it is in a try clause. a test for that would need to try every possible exception. it is impractical to do a test like that, so i did not try. but if you have a way to do it, i am interested. a file listing every possible exception might be usable as i could build a code sequence from that
i already know how to raise an exception and how to handle one. i do not know if anything different is done in an explicit raise inside a try clause that, while trying to optimize certain behavior, might end up making certain exceptions behave slightly different.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 158
Threads: 27
Joined: Jan 2019
Dec-17-2019, 05:43 AM
(This post was last modified: Dec-17-2019, 05:43 AM by Clunk_Head.)
(Dec-16-2019, 11:08 PM)Skaperen Wrote: i have tried nothing, yet. unless i can come up with a good test the covers all possible cases, i don't bother to waste my time trying it. for this, my concern is whether a direct raise might, for some exceptions, be handled different because it is in a try clause. a test for that would need to try every possible exception. it is impractical to do a test like that, so i did not try. but if you have a way to do it, i am interested. a file listing every possible exception might be usable as i could build a code sequence from that
i already know how to raise an exception and how to handle one. i do not know if anything different is done in an explicit raise inside a try clause that, while trying to optimize certain behavior, might end up making certain exceptions behave slightly different.
Sorry that you don't want to waste your time. I wouldn't expect anyone else to, either, given that it's your code.
That aside, There is no difference in handling a raised exception regardless of where it is raised. You should be able to determine every type of exception that your code can possible throw by examining each action that you take. For instance casting data types can throw a TypeError, the use of lists can produce an IndexOutOfBoundsException, Using dictionaries can produce a KeyError. However, you should not need to go through every possible type of exception.
Once you have accounted for each type of exception make an except to deal with it. Another user in the thread correctly suggested using a more exotic error type, which makes a lot of sense.
Give it a shot, it's not too difficult.
When you've got something post it if it doesn't work the way that you expect. I'm sure many here will help fix errors once there is code on which to work.
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
Dec-17-2019, 06:48 AM
(This post was last modified: Dec-17-2019, 06:48 AM by Skaperen.)
everyone is sure that doing a raise will be no real difference than such an exception really happening?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 101
Threads: 5
Joined: Jul 2019
If my in-take is correct then your ask is
try:
<condition>
raise <exception>
except <exception>:
<do whatever you want> If this is the case then we can add it even for existing exceptions
Posts: 1,838
Threads: 2
Joined: Apr 2017
(Dec-17-2019, 06:48 AM)Skaperen Wrote: everyone is sure that doing a raise will be no real difference than such an exception really happening?
Why would there be any difference? Using raise is the only way you have to, well, raise exceptions and try is the facility used to handle them, so I think you're over thinking here.
|