Python Exception and Errors

Python Exception and error

python error & built-in exceptions

There are many built-in python exception an interpreter raises whenever the extraordinary situation encounters in the program. 

People often gets confused with error and exception as both looks very similar but these two operates in a very different way.

Python Error

Error encounters in the program when something wrong with the code. In error situation, program execution gets terminated and there is no way to handle this case unless you don’t fix the error.

Most common error observed with programmer is – syntax error. Observe this example to understand how error looks like in python.

This example demonstrates syntax error with if statement. You can see, semicolon is missing in if statement.

var=100

if var>10

    print (“Variable – var=”, var)

  File “C:/Users/Documents/Python_raise.py”, line 2

    if var>10

            ^

SyntaxError: invalid syntax

Python built-in Exceptions

The built-in exceptions are raised by interpreter or built-in functions whenever an extraordinary error situation occur in the code. 

When an exception encounter in your code, program execution will get terminated unless you don’t handle specific situation in your code. HOW to handle an exception? Visit this page – Python try/except and finally Statements.

There is also possibility to raise built-in exception manually by using raise keyword. Visit this page to learn more about Python raise keyword.

[table id=1 /]

Read More : Python try except and finally statements.

Leave a Comment