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

program to look up price in dictionary

I am having issues with creating this program I don't know whether I should use elif or something else. Here is the question: In the cell below, use the try/except control structure to create a program which looks up a price in a…
-3
votes
2 answers

How to catch python syntax errors?

try: pattern=r'' except: try: pattern=r"
mlzboy
  • 14,343
  • 23
  • 76
  • 97
-3
votes
1 answer

how can i make code which is that input is a specific date and output is the day of the week?

i want to use Class and try~except function to make codes. input is that i put integer (ex: 20160101) then it converts to datetime. And final output is the day of the week. I think i can convert integer to date like this: >>>import datetime >>>d =…
Ellena
  • 1
-4
votes
0 answers

Why is the "except" catching the assertion error?

def foo(x): assert x!=0 return 1/x try: print(foo(0)) except ZeroDivisionError: print("zero") except: print("some") I think the output must be AssertionError. Can anyone explain this code? I think the expected output of this…
-4
votes
3 answers

How to print a message to the user when a negative number is inputed?

I am learning about the try and except statements now and I want the program to print out a message when a negative number is inserted but I don't know how to create the order of the statements for this output: print("how many dogs do you…
Ryan
  • 1
  • 1
-4
votes
2 answers

What are the unknown issues of using try/except block in functions in python?

I want to know the side effects or unknown issues of using try/except block in the below approaches? Approach 1: def f1(): try: # some code here except Exception as e: print(str(e)) def f2(): try: f1() …
ashish14
  • 650
  • 1
  • 8
  • 20
-4
votes
1 answer

Try except not catching error

I'm trying to add an error message if the input is non-numeric. I've tried try/except and now trying if/else but both don't get activated (e.g. if the user enters "ten percent", there is an error output versus my error message) The first one…
WW C
  • 11
  • 2
-4
votes
1 answer

Python - none returns from try and except clauses

the return from this code (date) is 'none' if an invalid entry is provided, then a valid entry is given. However if a valid entry is given first time, the correct return for date is given. Any tips on how I can solve it? def getDate(): date =…
-5
votes
1 answer

How to catch specific index error in Python and attach new value for this?

I want to return None only if i catch Index out of range for the value in issue def extract_values(values): try: first = values[0] second = values[1] last = values[3] except IndexError: first = "None" …
Dhayf OTHMEN
  • 71
  • 1
  • 8
-5
votes
1 answer

Try / Except Statements in python

I'm trying to create a python program that uses try/except statements. Everything works fine except, I'm still getting a value error, even after defining the except statement. my code: inventory = {847502: ['APPLES 1LB', 1.99, 50], 847283:…
KKH
  • 7
  • 6
-5
votes
1 answer

Invalid Syntax on Except TypeError?

why do I get an invalid syntax on except TypeError and how do I fix it? Try: score=float(raw_input("What is your score?")) If score>100 or score<0: print "please enter a score between 100 and 0" except TypeError: print "Please enter your score in…
-5
votes
2 answers

understanding "try" and "except" python

For what I have understand till now is, computer will try to run the code in try section and will except stuff mentioned in except part. As soon as computer got something as mentioned in except it will run except code. So, I tried the…
Freddy
  • 2,216
  • 3
  • 31
  • 34
-6
votes
1 answer

syntax error in python 3.4

I am getting a syntax error when I use except: even for simple try: a=0 except: statement try : a=0 print(a + 2) except : And the error is SyntaxError: invalid syntax
shiller
  • 19
  • 2
1 2 3
59
60