Errors and Exceptions in Python

There are two kinds of errors in Python: syntax error and exceptions.

Syntax Error

Syntax errors are the errors that are caused by not following the proper structure (syntax) of the language. They are also known as parsing error.

When you forget a colon at the end of a line, accidentally add one space too many when indenting under an if statement, or forget a parenthesis, you will encounter a syntax error. This means that Python couldn’t figure out how to read your program. This is similar to forgetting punctuation in English. 



def some_function()
    msg = "hello, world!"
    print(msg)
     return msg


The above function definition has two issues with syntax. First one is that the function definition is missing a colon (:) at the end. The second one is that the lines in the function definition do not all have the same indentation (IndentationError).

Exceptions

Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions.

Exceptions come in different types, and the type is printed as part of the message. For example, when a file we try to open does not exist (FileNotFoundError), dividing a number by zero (ZeroDivisionError), module we try to import is not found (ImportError) etc.

Whenever these type of run-time error occur, Python creates an exception object. If not handled properly, it prints a traceback to that error along with some details about why that error occurred.

We can also define our own exception in Python (if required). These are called user-defined exceptions.

We can handle these built-in and user-defined exceptions in Python by using try, except and finally statements.

Let me give an example of two of the exceptions in Python.

TypeError: Raised when a function or operation is applied to an object (or between objects) of incorrect (incompatible) data type.
ValueError: Raised when a function or operation gets argument of correct data type but improper value.

Python allows Type Casting where you can create object of one data type from the object of another data type. But there are some restrictions in this process that if not followed will generate an illegal operation, leading into Python exception.



>>> float(10 + 20j)
Traceback (most recent call last):
  File "", line 1, in 
    float(10 + 20j)
TypeError: can't convert complex to float  ==> Complex data type is incompatible for float() function.
>>>
>>> float("Ten.OneNinetyThree")
Traceback (most recent call last):
  File "", line 1, in 
    float("Ten.OneNinetyThree")
ValueError: could not convert string to float: 'Ten.OneNinetyThree'  ==> str data type is compatible for float() function but 'Ten.OneNinetyThree' is an improper value.
>>>
>>> float("10.193")
10.193  ==> This is OK.
>>>


Further Readings

Python Errors and Built-in Exceptions 

Errors and Exceptions 

Comments

Back To Top

Popular posts from this blog

How to save video from Internet Explorer

error 18 at 0 depth lookup: self signed certificate

How to check fragmentation in MySQL tables