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
7
votes
1 answer

Can't catch ValueError in Python

I am starting to learn Python, and I wrote a very simple code to practice try/except. Here is the code: a = float(input('num1: ')) b = float(input('num2: ')) try: result = a / b except ValueError as e: print ('error type: ', type…
idnfsp
  • 73
  • 1
  • 1
  • 4
7
votes
2 answers

Catching all exceptions without pylint error

import traceback def func(): try: -- do something -- except: traceback.print_exc() For this code pylint reporting error: bare-except No exception type(s) specified , W0702, Occurs when an except clause doesn't specify…
sanapala mohanarao
  • 321
  • 1
  • 2
  • 16
7
votes
2 answers

Try and Except catch all errors, except for sys.exit()

I have created a function, but errors could pop up. That is why I want to use exceptions to generalize all errors into the same message. However, this function contains multiple sys.exit() calls. As a result, I would like to have my code jump into…
James the Great
  • 941
  • 2
  • 14
  • 46
6
votes
1 answer

Why is except not catching this error?

I have a program that simulates dice rolls and compares them to values in a chart (set of String lists). I currently get the value from a TEdit. If the box is empty it raises a EConvertError that should be caught by my Try/Except statement, but it's…
Aaron
  • 896
  • 3
  • 11
  • 22
6
votes
5 answers

A DRY approach to Python try-except blocks?

Objective: I have several lines of code each capable of producing the same type of error, and warranting the same kind of response. How do I prevent a 'do not repeat yourself' problem with the try-except blocks. Background: I using ReGex to scrape…
Michael Molter
  • 1,296
  • 2
  • 14
  • 37
6
votes
5 answers

Better way to check a list for specific elements - python

I am using try/except blocks as a substitute for if/elif that has a bunch of ands. I am looking into a list and replacing some elements if it has x and x and x, etc. In my project, I have to check for upwards of 6 things which drew me to using the…
Iluvatar14
  • 699
  • 1
  • 5
  • 18
6
votes
1 answer

very simple code, and getting error C2712, could not understand why

I'm having trouble for a while with error C2712: Cannot use __try in functions that require object unwinding, after narrowing the problem, I was left with a very very simple code, and i can not understand why it causes this error. I am using Visual…
user1438233
  • 1,153
  • 1
  • 14
  • 30
6
votes
8 answers

Why use exception handling in apparently "safe" code?

Please, may somebody explain me, what can raise an exception in this code? function CreateBibleNames: TStrings; begin Result := TStringList.Create; try Result.Add('Adam'); Result.Add('Eva'); Result.Add('Kain'); …
lyborko
  • 2,571
  • 3
  • 26
  • 54
6
votes
2 answers

in python , why is there a difference in exception when signal handling is done before and after "try except"

I recently started with python . I was playing with handling the keyboard interrupt , when I came across this behavior import signal,sys def handleInt(sign,no): print "interrupted" signal.signal(signal.SIGINT,handleInt) # exception raised…
karyboy
  • 317
  • 1
  • 5
  • 22
5
votes
1 answer

In python 3, re-raise an error with a shorter traceback

I'm trying to create a try-except block that re-raises an arbitrary exception, but only includes the final block in the traceback stack. Something like this: import traceback def my_func(shorten_tracebacks=True): try: # Do stuff here …
Abe
  • 22,738
  • 26
  • 82
  • 111
5
votes
1 answer

What is an equivalent of "except socket.error as (code, msg)" in Python3?

I have this try/except block in Python2 that does not run in Python3 due to the line except socket.error as (code, msg): try: (conn, (ip,port)) = tcpServer.accept() except socket.error as (code, msg): if code != errno.EINTR: raise …
roschach
  • 8,390
  • 14
  • 74
  • 124
5
votes
1 answer

python exception not raised if finally returns value

Can anyone explain why the following example does not raise the Exception? def foo(): try: 0/0 except Exception: print('in except') raise finally: print('in finally') return 'bar' my_var =…
ezdazuzena
  • 6,120
  • 6
  • 41
  • 71
5
votes
1 answer

do something when terminate a python program

I want make a python script do something such as publishing a message before terminating it. I have tried signal and it works when I input Ctrl+c in cmd window. However, it seems inoperative when I just directly close cmd window or kill the process…
Da Vinci
  • 51
  • 2
5
votes
4 answers

Issues with try/except, attempting to convert strings to integers in pandas data frame where possible

I made a function to clean up any HTML code/tags from strings in my dataframe. The function takes every value from the data frame, cleans it with the remove_html function, and returns a clean df. After converting the data frame to string values and…
RF_PY
  • 343
  • 1
  • 3
  • 9
5
votes
5 answers

Exception AttributeError: "'NoneType' object has no attribute 'path'" in

I am debugging python code (python2.7.12) as my code works but I get NULL for all variables when streaming tweets into the database. The error I got is: Exception AttributeError: "'NoneType' object has no attribute 'path'" in
wannabhappy
  • 531
  • 2
  • 5
  • 7
1 2
3
59 60