Questions tagged [try-except]

A form of error handling in Python and Delphi, similar to try/catch in C-like languages. It is also a Microsoft extension in C and C++.

Try/Except is a form of error handling in Python and Delphi, similar to try/catch in C-like languages. It is also a Microsoft extension in C and C++.

Links

Error Handling in Python
Except Keyword in Delphi
Try/Except in Microsoft C and C++

898 questions
17
votes
9 answers

Why doesn't this Python keyboard interrupt work? (in PyCharm)

My Python try/except loop does not seem to trigger a keyboard interrupt when Ctrl + C is pressed while debugging my code in PyCharm. (The same issue occurs when using Ctrl + C while running the program, but not in the PyCharm Python console.) My…
Edwin Shepherd
  • 413
  • 1
  • 8
  • 17
17
votes
3 answers

Several `with`s in `try`s

I have several possible files which could hold my data; they can be compressed in different ways, so to open them I need to use file(), gzip.GzipFile() and other which also return a file object (supporting the with interface). I want to try each of…
Alfe
  • 56,346
  • 20
  • 107
  • 159
14
votes
3 answers

Is it correct to use a return statement within a try and except statement?

If I have such code in the end of function: try: return map(float, result) except ValueError, e: print "error", e Is it correct to use try / except in return part of method? Is there a wiser way to solve this?
smith
  • 371
  • 2
  • 5
  • 14
12
votes
3 answers

try-finally in Python 3 generator

I have met a snippet of Python 3 code: def gen(): try: while True: yield 1 finally: print("stop") print(next(gen())) After I run it, I thought at first that the output should be: 1 But actually the result…
ruanhao
  • 4,663
  • 6
  • 28
  • 43
11
votes
2 answers

Catching specific error messages in try / except

I am new to python and I am about to make this new program that will ask you for your birthday. I've made some try/except clauses to avoid people entering info in strings or to high numbers. I would like my program to find out if the info entered…
11
votes
3 answers

Calculating Precision, Recall and F-score in one pass - python

Accuracy, precision, recall and f-score are measures of a system quality in machine-learning systems. It depends on a confusion matrix of True/False Positives/Negatives. Given a binary classification task, I have tried the following to get a…
alvas
  • 115,346
  • 109
  • 446
  • 738
10
votes
1 answer

Why does code that in 3.10 throws a RecursionError as expected not throw in earlier versions?

To start I tried this def x(): try: 1/0 # just an division error to get an exception except: x() And this code behaves normally in 3.10 and I get RecursionError: maximum recursion depth exceeded as I expected but 3.8 goes into a…
10
votes
1 answer

Delphi - What is the "correct" order for except and finally blocks?

Suppose I have the following routine: function ReadFile(f : TFilename) : Boolean; var fs : TFileStream; begin Result := False; try fs := TFileStream.Create(f, ...); try // read file ... Result := True; finally …
rossmcm
  • 5,493
  • 10
  • 55
  • 118
10
votes
3 answers

Is it faster to check a file's existence prior to loading, or to catch the exception when it doesn't exist?

I have been recommended to use the second, try-except variant, but I would also like to know what others think: which procedure of the two below (if any) is more time-efficient? procedure LoadImage(img: TImage; filename: string); begin if…
BogdyBBA
  • 101
  • 4
9
votes
5 answers

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? Example of text file: 0 1 0 2 nope 1 3 2 5…
Hal
  • 372
  • 4
  • 13
9
votes
3 answers

Where to put exception handling in python

When using try/except blocks in Python, is there a recommendation to delegate it to any methods that might raise an exception, or to catch it in the parent function, or both? For example, which of the following is preferred? def my_function(): s…
mcy
  • 155
  • 8
8
votes
5 answers

Ignore exception in Python

I have try-except block in python, and I want to do nothing when exception occurs. My code is as follows: for i in range(len(grid)): for j in range(len(grid[i])): try: count = count > int(grid[i][j]) ? count :…
hrishikeshp19
  • 8,838
  • 26
  • 78
  • 141
8
votes
4 answers

Porting VC++'s __try/__except EXCEPTION_STACK_OVERFLOW to MinGW

I am trying to port some code using VC++'s try-except statement to MinGW: bool success = true; __try { //... } __except ((EXCEPTION_STACK_OVERFLOW == GetExceptionCode()) ? EXCEPTION_EXECUTE_HANDLER :…
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
8
votes
5 answers

Try two expressions with `try except`

I have two expressions. I need to try one expression, if it is raise an exception try another, but if the second raises an exception too - to raise the exception. I tried this, but it is looks ugly and I am not sure it is the best way to solve this…
I159
  • 29,741
  • 31
  • 97
  • 132
7
votes
1 answer

python exit infinite while loop with KeyboardInterrupt exception

My while loop does not exit when Ctrl+C is pressed. It seemingly ignores my KeyboardInterrupt exception. The loop portion looks like this: while True: try: if subprocess_cnt <= max_subprocess: try: notifier.process_events() …
sadmicrowave
  • 39,964
  • 34
  • 108
  • 180
1
2
3
59 60