About 172,000 results
Open links in new tab
  1. python - How can I catch multiple exceptions in one line? (in the ...

    import sys try: mainstuff() except (KeyboardInterrupt, EOFError), err: # don't do this in Python 2.6+ print err print err.args sys.exit(0) If you see the comma name assignment in your …

  2. Are nested try/except blocks in Python a good programming …

    Jun 10, 2013 · A good and simple example for nested try/except could be the following: import numpy as np def divide(x, y): try: out = x/y except: try: out = np.inf * x / abs(x) except: out = …

  3. python - Difference between except: and except Exception as e:

    Apr 6, 2021 · Both the following snippets of code do the same thing. They catch every exception and execute the code in the except: block. Snippet 1 - try: #some code that may throw an …

  4. How do I print an exception in Python? - Stack Overflow

    import traceback try: 1/0 except Exception: traceback.print_exc() Output: Traceback (most recent call last): File "C:\scripts\divide_by_zero.py", line 4, in <module> 1/0 ZeroDivisionError: …

  5. Python try except - Stack Overflow

    Aug 24, 2024 · Upon an exception being raised control leaves the try block at the point the exception is raised and is given to the appropriate except block. If statement 1 throws an …

  6. Manually raising (throwing) an exception in Python

    Jun 13, 2022 · If we look at what tthe dis dissasembely module for python shows, we can see the bytecode. 2 LOAD_CONST 1 (0) 4 LOAD_CONST 1 (0) 6 BINARY_OP 11 (/) So essentialy …

  7. Cost of exception handlers in Python - Stack Overflow

    In another question, the accepted answer suggested replacing a (very cheap) if statement in Python code with a try/except block to improve performance. Coding style issues aside, and …

  8. python - One try block with multiple excepts - Stack Overflow

    In Python, is it possible to have multiple except statements for one try statement? Such as: try: #something1 #something2 except ExceptionType1: #return xyz except ExceptionType2: #...

  9. Using 'try' vs. 'if' in Python - Stack Overflow

    In Python 3, try/except was 25 % faster than if key in d: for cases where the key was in the dictionary. It was much slower when the key wasn't in the dictionary, as expected, and …

  10. exception - Catch any error in Python - Stack Overflow

    Jul 25, 2011 · Using except by itself will catch any exception short of a segfault. try: something() except: fallback() You might want to handle KeyboardInterrupt separately in case you need to …

Refresh