0% found this document useful (0 votes)
4 views3 pages

5 Python - Error Handling

The document explains the differences between errors and exceptions in Python, highlighting that errors are serious issues that prevent program execution, while exceptions are runtime problems that can be handled with code. It categorizes errors into syntax errors, runtime errors, and logical errors, detailing their causes and handling methods. The document emphasizes that all exceptions are errors, but not all errors qualify as exceptions.

Uploaded by

Arun Sinha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

5 Python - Error Handling

The document explains the differences between errors and exceptions in Python, highlighting that errors are serious issues that prevent program execution, while exceptions are runtime problems that can be handled with code. It categorizes errors into syntax errors, runtime errors, and logical errors, detailing their causes and handling methods. The document emphasizes that all exceptions are errors, but not all errors qualify as exceptions.

Uploaded by

Arun Sinha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Error Handling 1

Error and Exception

In Python, errors and exceptions are both problems that occur during
program execution, but they are not exactly the same.

Error An error is a broader term for any issue that prevents a


program from running correctly. Errors are usually serious problems that a
program should not try to handle.
 Often caused by syntax mistakes or fundamental issues
 Occur before or during execution
 Typically not recoverable

Example : if x == 10
print(x) // This gives a SyntaxError because of
missing : (colon)

Exception An exception is a specific type of error that occurs during


program execution and can be handled using code.
 Occurs at runtime
 Can be handled using try, except, finally
 Allows program to continue running

Example : try:
Int_Amt = Principle * int_rate / percentage //
percentage = 0
except ZeroDivisionError:
print("Cannot divide by zero")

Feature Error Exception


Meaning Serious problem Runtime problem
Occurren Compile-time or runtime Runtime only
ce
Handling Not usually handled Can be handled with try-
except
Type Broad category Subclass of errors
Example SyntaxError, ZeroDivisionError,
IndentationError ValueError

All exceptions are errors, but not all errors are exceptions.
Python Error Handling 2

Types of Errors in Python


---------------------------------
Primarily of two types – Syntax errors and Runtime errors (Exceptions, as
above)
A third type can be – Logical errors

1) Syntax Errors - A syntax error occurs when the code violates the rules
(grammar) of Python. Python cannot understand the code, so the program
fails before execution. Some of the probable causes can be : Missing colon,
Incorrect indentation, Misspelled keywords, Missing brackets or quotes
etc.

Syntax errors must be fixed before the program can run

2) Runtime Errors (Exceptions) - A run-time error occurs during program


execution.
The syntax is correct, but so nothing goes wrong while running the
program. Probable situations are – division by zero, file not found,
accessing invalid index, invalid type operations, wrong value entered by
User etc.

Such exceptions can be handled in multiple ways (next chapter).

3) Logical Errors - A logical error occurs when the program runs without
crashing, but produces incorrect output. Probable causes can be - wrong
formula, incorrect condition, mistakes in algorithm etc.

Example : a = 10
b = 20
avg = a + b / 2
print(avg) //20

Logical errors are hardest to detect because: Program runs successfully


and No error message is shown

Type of When it Can Program Can Be Example


Error Occurs Run? Handled?
Python Error Handling 3

Syntax Error Before ❌ No ❌ No Missing :


execution
Run-time During Stops ✅ Yes Division by
Error execution midway zero

Logical After ✅ Yes ❌ Not directly Wrong


Error execution formula

You might also like