Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2.7 exception handling syntax

I am bit confused about the try exception usage in Python 2.7.

try:
    raise valueError("sample value error")
except Exception as e:
    print str(e)    

try:
    raise valueError("sample value error")
except Exception,exception:
    print str(exception)


try:
    raise valueError("sample value error")
except exception:
    print str(exception)


try:
    raise valueError("sample value error")
except Exception:
    print str(Exception) # it prints only the object reference

can some help me to understand the above usage?

like image 200
tamil Avatar asked Sep 16 '15 15:09

tamil


People also ask

What is Python exception syntax?

An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions. In general, when a Python script encounters a situation that it cannot cope with, it raises an exception. An exception is a Python object that represents an error.

Which of the syntax is correct in exception handling?

TryCatch and Try::Tiny both allow use of try/catch/finally syntax instead of boilerplate to handle exceptions correctly.

How do you handle syntax error using exception handling in Python?

Python Try Except Else Example try: <--program code--> except <--Exception Type 1-->: <--exception handling code--> except <--Exception Type 2-->: <--exception handling code--> except <--Exception Type 3-->: <--exception handling code--> ... else: <--program code to run if "try" block doesn't encounter any error--> ...

What is the syntax for raising an exception?

The raise keyword is used to raise an exception. You can define what kind of error to raise, and the text to print to the user.


3 Answers

Some concepts to help you understand the difference between the alternate variants of the except variants:

  • except Exception, e – This in an older variant, now deprecated, similar to except Exception as e
  • except Exception as e – Catch exceptions of the type Exception (or any subclass) and store them in the variable e for further processing, messaging or similar
  • except Exception – Catch exceptions of the type Exception (or any subclass), but ignore the value/information provided in the exception
  • except e – Gives me an compilation error, not sure if this related to python version, but if so, it should/would mean that you don't care about the type of exception but want to access the information in it
  • except – Catch any exception, and ignore the exception information

What to use, depends on many factors, but if you don't need the provided information in the exception there is no need to present the variable to catch this information.

Regarding which type of Exception to catch, take care to catch the accurate type of exceptions. If you are writing a general catch it all, it could be correct to use except Exception, but in the example case you've given I would opt for actually using except ValueError directly. This would allow for potentially other exceptions to be properly handled at another level of your code. The point is, don't catch exception you are not ready to handle.

If you want, you can read more on python 2.7 exception handling or available python 2.7 exception in the official documentation.

like image 191
holroy Avatar answered Oct 18 '22 02:10

holroy


For Python 3 (also works in Python 2.7):

try:
      raise ValueError("sample value error")
except Exception as e:
      print(e)

For Python 2 (will not work in Python 3):

try:
      raise ValueError("sample value error")
except Exception, e:
      print e
like image 27
abduljalil Avatar answered Oct 18 '22 01:10

abduljalil


I use:

try:
    raise valueError("sample value error")
except Exception as e:
    print str(e) 

When I want to declare a specific error and

try:
    raise valueError("sample value error")
except:
    print "Something unexpected happened"

When I don't really care or except: pass , except: return etc

like image 42
Rolf of Saxony Avatar answered Oct 18 '22 03:10

Rolf of Saxony