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
5
votes
4 answers

Try Except float but not integer

So, I've approached an obstacle in a programming exercise. I understand the concept of try except but how can I use a try except handler to only accept a float or decimal and if a whole number or integer is entered, it throws an error message. I…
Gallieon474
  • 55
  • 1
  • 7
5
votes
1 answer

How to trace back the cause of an exception raised within a function?

(This is a follow-up question to the post Python try/except: Showing the cause of the error after displaying my variables.) I have the following script.py: import traceback def process_string(s): """ INPUT ----- s: string …
4
votes
5 answers

c++ try-except statement

I came across this article about detecting VMWare or Virtual PC http://www.codeproject.com/KB/system/VmDetect.aspx and I saw that they use some kind of try-except statement. So I looked it up in the MSDN:…
Idov
  • 5,006
  • 17
  • 69
  • 106
4
votes
3 answers

How to say in Pythonese - do something unless it causes an error (without resorting to multilevel try/execpt blocks)

This is a little difficult to explain, so let's hope I'm expressing the problem coherently: Say I have this list: my_list = ["a string", 45, 0.5] The critical point to understand in order to see where the question comes from is that my_list is…
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
4
votes
3 answers

Python Unit Test for Try/Except Block

I have the following function with unit tests: #!/usr/bin/env python3 # https://docs.python.org/3/library/ipaddress.html # https://docs.python.org/3.4/library/unittest.html import ipaddress import unittest from unittest.mock import patch from…
marshki
  • 175
  • 1
  • 11
4
votes
4 answers

python try except as a function to evaluate expressions

I have tried creating a function that tries an expression and returns zero if errors are risen. def try_or_zero(exp): try: exp return exp except: return 0 Which obviously doesn't work. It seems the problem is that…
Victor Sg
  • 153
  • 1
  • 1
  • 11
4
votes
3 answers

Try except recursion or while loop?

I am doing a python course where they suggested a try and except block in a while loop in order to keep asking for input until the condition is satisfied. Intuitively I feel it is shorter to just call the function again in the "except" block like…
4
votes
3 answers

Python selenium CTRL+C closes chromedriver

How can I catch CTRL+C (a KeyboardInterrupt) without causing the chromedriver to close. It closes the chromedriver when I run my script.py through cmd. driver = webdriver.Chrome() try: while True: #do stuff with chromedriver except…
4
votes
1 answer

Is it possible to add a timeout in try block in python

I am trying to download a URL using python-wget downloaded from : https://pypi.python.org/pypi/wget This package does not support a timeout option, hence it takes some time (around 10seconds) roughly for a query to fail. Is it possible to add a…
ashdnik
  • 648
  • 3
  • 8
  • 20
4
votes
1 answer

How can i add the functions try/except in this code?

I'm still creating this code where via a dictionary attack i find a password, inserted by the user. However I would insert some controls in the input of the file's source (ex. when I type the source of a file that doesn't exist) and when I open a…
HaCk81t
  • 41
  • 2
4
votes
4 answers

try-except block: analogue for 'else' if exception was raised

I have this kind of code: try: return make_success_result() except FirstException: handle_first_exception() return make_error_result() except SecondException: handle_second_exception() return make_error_result() And I'm…
3Gee
  • 1,094
  • 2
  • 14
  • 23
4
votes
1 answer

Try-except for unknown function?

I'm trying to make a function that will test if a function exists or not, and then return a boolean value based on if it exists or not. Here's my code; however, Python IDLE 3.5 tells me that there is an error with my eval() statement, but I don't…
richzli
  • 131
  • 8
4
votes
2 answers

Ctrl-C ends my script but it is not caught by KeyboardInterrupt exception

I have a Python script that contains a big loop reading a file and doing some stuff (I am using several packages like urllib2, httplib2 or BeautifulSoup). It looks like this : try: with open(fileName, 'r') as file : for i, line in…
Thematrixme
  • 318
  • 1
  • 4
  • 14
4
votes
3 answers

Error handlers in python

I have a trouble to find the "pythonic" way to do this: I need to catch different blocks of code with the same try-except pattern. The blocks to catch are different from each other. Currently I am repeating the same try-except pattern in several…
Garet
  • 365
  • 2
  • 13
4
votes
2 answers

try except unnecessary step

Is it always safe to use hello2 instead of hello1 ? def hello1(): try: aaa = foo() return aaa except baz: return None def hello2(): try: return foo() except baz: return None
dola
  • 201
  • 4
  • 9