Click to join our Telegram group: https://t.
me/+leBJyZDDoUw3NWRl
Class 12 Computer Science
Chapter 3: Exception Handling
What is Debugging?
Debugging is the process of finding and fixing errors or bugs in the source code of any
software.
Errors are of three types – • Compile Time Error • Run Time Error • Logical Error
1. Compile time error: These errors are basically of 2 types –
1. Syntax Error: Violation of formal rules of a programming language results in
syntax error also known as parsing errors. example: improper closing bracket,
missing colon (:), missing parentheses () with print(),use of semicolon instead
of comma, improper indentation
Print (10), print “hello”
2. Semantics Error: Semantics refers to the set of rules which sets the meaning of
statements. A meaningless statement results in semantics error.
x*y=z
2. Run time Error: These errors are generated during a program execution due to
resource limitation. Such type of error are also termed as exceptions.
Even if a statement or expression is syntactically correct, there might arise an error
during its execution.
Ex: Division by zero, using variable which has not been defined, accessing a list
element which doesn’t exist, try to access a file which doesn’t exit.
3. Logical Error: If a program is not showing any compile time error or run time error
but not producing desired output, it may be possible that program is having a logical
error.
ex: Using wrong operators like using // in place of /, giving wrong operator
precedence.
Some Common Python Exceptions
NameError: This type of exception occurs when you are trying to use a variable that
doesn’t exist in the current environment.
TypeError: This type of error is raised when an operation or function is attempted
that is invalid for the specified data type.
ValueError: This exception is raised when we try to give an incorrect type of value in
any function or method.
ZeroDivisionError: This exception is raised when division or modulo by zero takes
place for all numeric types.
Attribute Error: This exception is raised when an object does not find an attribute.
Key Error: This type of error occurs when you are trying to access an element of a
dictionary using a key that the dictionary does not contain.
Index Error: The index you are using to access a list, string, or tuple is greater than its
length minus one.
IOError: This exception occurs whenever there is an error related to input/output
such as opening a file that does not exist, trying to delete a file in use.
IndentationError: This type of exception is raised due to incorrect indentation while
typing if-else statements or in looping statements (compound statements) or even in
defining user-defined functions/ modules.
What is exception Handling
Exception handling is the process of responding to unwanted or unexpected events
when a computer program runs.
Methods for exception handling are:
For exception handling we have to follow given syntax:
Syntax:
try:
Write all code here which have chance to get exception…..
Except Exception Name I:
If there is an exception, then execute this block.
Except Exception Name II:
If there is an exception, then execute this block.
else:
If there is no exception, then execute this block
finally:
Always executed.
*Here try and except are main blocks, and we can do this code with single except
block also.
Example: