Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Are Python Exceptions runtime errors?

If I run the code given below

def myfunction(x, y):
      return x + y
else:
    print("Hello!")

I get an error. Is it a python RuntimeError?


1 Answer
Rajendra Dharmkar

All python exceptions are not runtime errors, some are syntax errors as well.

If you run the given code, you get the following output.

File "C:/Users/TutorialsPoint1/~.py", line 4
else:
^
SyntaxError: invalid syntax

We see that it is syntax error and not a runtime error.

Errors or inaccuracies in a program are often called as bugs. The process of finding and removing errors is called debugging. Errors can be categorized into three major groups:

  1. Syntax errors 2. Runtime errors and 3. Logical errors

Syntax errors

Python will find these kinds of errors when it tries to parse your program, and exit with an error message without running anything. Syntax errors are like spelling or grammar mistakes in a language like English.

Runtime errors

If a program is free of syntax errors, it will be run by the Python interpreter. However, the program may exit if it encounters a runtime error – a problem that went undetected when the program was parsed, but is only revealed when the code is executed.

Some examples of Python Runtime errors:

  1. division by zero
  2. performing an operation on incompatible types
  3. using an identifier which has not been defined
  4. accessing a list element, dictionary value or object attribute which doesn’t exist
  5. trying to access a file which doesn’t exist



Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.