Python Errors and exceptions | Vskills Tutorials

Python Errors and exceptions

Errors

Consider a simple print function call. What if we misspelt print as Print? Note the capitalization. In this case, Python raises a syntax error.

   >>> Print('Hello World')
   Traceback (most recent call last):
     File "<pyshell#0>", line 1, in <module>
       Print('Hello World')
   NameError: name 'Print' is not defined
   >>> print('Hello World')
   Hello World

Observe that a NameError is raised and also the location where the error was detected is printed. This is what an error handler for this error does.

Exceptions

We will try to read input from the user. Press ctrl-d and see what happens.

   >>> s = input('Enter something --> ')
   Enter something --> 
   Traceback (most recent call last):
     File "<pyshell#2>", line 1, in <module>
       s = input('Enter something --> ')
   EOFError: EOF when reading a line

Python raises an error called EOFError which basically means it found an end of file symbol (which is represented by ctrl-d) when it did not expect to see it.

-Swaroopch

Apply for Govt Certified Python Certification Now!!

Go back to Tutorial                                                                                Go to Home Page

Get industry recognized certification – Contact us

Menu