CHAP. 1:: Exception Handling in Python
CHAP. 1:: Exception Handling in Python
CHAP. 1:: Exception Handling in Python
1 :
EXCEPTION HANDLING IN
PYTHON
Exception or ERROR
2. Semantic Errors
3. TypeErrors
5. Logical Errors.
1. Syntax Errors
) Missing
2. Semantic Errors
Semantics error occur when statements
are not meaningful
Semantics refers to the set of rules
which give the meaning of the
statement.
For example,
Rama plays Guitar
This statement is syntactically
and semantically correct and it has
some meaning.
2. Semantic Errors
See the following statement,
Guitar plays Rama
is syntactically correct (syntax is correct) but
semantically incorrect. Similarly, there are
semantics rules of programming language,
violation of which results in semantical
errors.
X*Y=Z
will result in semantical error as
expression can not ancome on the left side
of an assignment statement.
3. Type Error
Data in Pythpn has an associated data
type. The value 7, for instance, is an
integer and ‘a’ is a character constant
“Hi” is a string. If a function is given
wrong type of data, then Type Error is
assigned by compiler
For Example :
>>a=“Hi”;
>>a**2
This will
result in
3. Type Error
4. Run Time Errors.
A Run time error is that during
occurs
execution of the program.It
because is of some illegal caused
place. operation taking
For example
1.If a program is trying to open a file which
does not exists or it could not be
opened(meaning file is corrupted), results
into an execution error.
2.An expression is trying to divide a number
by zero are RUN TIME ERRORS.
5. Logical Errors.
• A Logical Error is that error which is
causes a program to produce incorrect
or undesired output.
for instance,
ctr=1;
while(ctr>10)
print(n *ctr)
ctr=ctr+1;
Exceptions
Exceptions
What is an exception?
For Example
Built-in exceptions
Commonly occurring exceptions defined
in the compiler/interpreter.
Built-in exceptions in Python
Syntax Error:
It is raised when there is an error in the syntax of
the Python code.
Keyboard Interrupt:
It is raised when the user accidentally hits the
Delete or Esc key while executing a program due to
which the normal flow of the program is interrupted.
FileNotFound Error
If the file cannot be found.
ValueError
Raised when a built-in operation or function
receives an argument that has the right type but
an inappropriate value.
EOF Error:
It is raised when the end of file condition is
reached without reading any data by input().
Index Error:
It is raised when the index or subscript in
a sequence is out of range.
Name Error:
It is raised when a local or global variable
name is not defined.
Indentation Error:
It is raised due to incorrect indentation in
the program code.
Type Error:
It is raised when an operator is supplied
with a value of incorrect data type.
Syntax:
raise exception-name[(optional argument)]
assert Expression[,arguments]
Python evaluates the expression given immediately after the
assert keyword. If this expression is false, an AssertionError
exception is raised which can be handled like any other
exception.
Ex. :
print("use of assert statement")
def negativecheck(number):
assert(number>=0), "OOPS... Negative Number“
print(number*number)
print (negativecheck(100))
print (negativecheck(-350))
Handling Exceptions
Each and every exception has to be handled by the
programmer to avoid the program from crashing
abruptly.
Exception handlers separate the main logic of the program from the
error detection and correction code.
The object is handed over to the runtime system so that it can find an
appropriate code to handle this particular exception.
This process of creating an exception object and handing it over to the runtime
system is called throwing an exception.