Linked Questions

1 vote
1 answer
773 views

NameError: name 'variable_name" is not defined when i try to catch an execption [duplicate]

Hello I am trying to create a program that reads two txt.files and displays them for the user on the console. I want to write an exception for the case that atleast one of the files is not in the same ...
bernd_singer_TL's user avatar
-3 votes
1 answer
48 views

What is the use of try and except inside a function in python? [duplicate]

I want to know how to use try and except inside a function and what change will it bring to my syntax of code. I was trying to use this tags inside one function but error was coming and my program was ...
Sushil Kumar Verma's user avatar
0 votes
0 answers
27 views

What is the best practice with regards to type consistency for returning a result, error, or warning in Python? [duplicate]

This might be answered elsewhere, but I had trouble finding a good "Googlable" phrasing to this question. I have a Python program I'm writing where the lead has requested that all exits from ...
Grant Curell's user avatar
  • 1,873
3313 votes
11 answers
3.3m views

Manually raising (throwing) an exception in Python

How do I raise an exception in Python so that it can later be caught via an except block?
TIMEX's user avatar
  • 274k
241 votes
7 answers
40k views

When is .then(success, fail) considered an antipattern for promises?

I had a look at the bluebird promise FAQ, in which it mentions that .then(success, fail) is an antipattern. I don't quite understand its explanation as for the try and catch. What's wrong with the ...
user2127480's user avatar
  • 4,749
132 votes
6 answers
562k views

re.sub erroring with "Expected string or bytes-like object"

I have read multiple posts regarding this error, but I still can't figure it out. When I try to loop through my function: def fix_Plan(location): letters_only = re.sub("[^a-zA-Z]", # ...
imanexcelnoob's user avatar
71 votes
9 answers
64k views

What's the best way to handle Django's objects.get?

Whenever I do this: thepost = Content.objects.get(name="test") It always throws an error when nothing is found. How do I handle it?
TIMEX's user avatar
  • 274k
36 votes
15 answers
10k views

Are exceptions really for exceptional errors? [closed]

It's my understanding that common wisdom says to only use exceptions for truly exceptional conditions (In fact, I've seen that statement here at SO several times). However, Krzysztof Cwalina says: ...
Esteban Araya's user avatar
8 votes
2 answers
35k views

Python OpenCV Error: "TypeError: Image data cannot be converted to float"

So I am trying to create a Python Program to detect similar details in two images using Python's OpenCV. I have the two images and they are in my current directory, and they exist (see the code in ...
user avatar
9 votes
3 answers
23k views

Check if key exists and get the value at the same time in Python?

Sometimes we may need to verify the existence of a key in a dictionary and make something if the corresponding value exists. Usually, I proceed in this way: if key in my_dict: value = my_dict[...
Delgan's user avatar
  • 19.9k
9 votes
5 answers
8k views

Other options instead of using try-except

When line 2 in the text file has 'nope' then it will ignore the line and continue the next one. Is there another way to write this without using try and except? Can I use if else statement to do this? ...
Hal's user avatar
  • 372
17 votes
2 answers
8k views

Why does handling multiple exceptions require a tuple, but not a list?

Consider the following example: def main_list(error_type): try: if error_type == 'runtime': raise RuntimeError("list error") if error_type == 'valueerror': ...
enderland's user avatar
  • 14.2k
3 votes
3 answers
2k views

try catch or type conversion performance in julia - (Julia 73 seconds, Python 0.5 seconds)

I have been playing with Julia because it seems syntactically similar to python (which I like) but claims to be faster. However, I tried making a similar script to something I have in python for ...
chase's user avatar
  • 3,782
2 votes
3 answers
6k views

How to multiply big numbers faster?

I was experimenting with multiplying large numbers in python. For my purpose I was trying to evaluate. 2*odd*(4*odd^2 + 7)/3 - 6*odd^2 - 3 Now my question boils down to how to multiply numbers ...
N3buchadnezzar's user avatar
1 vote
4 answers
4k views

Variable assignment from unknown length list in Python

I'm scraping a website in order to store data in a database that has 3 columns. The part of the webstsite i'm scraping looks like one of either of the three examples below # Example 1: <div> &...
exhoosier10's user avatar

15 30 50 per page