0% found this document useful (0 votes)
8 views

Part3 SW SEC Exceptional Handling

Uploaded by

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

Part3 SW SEC Exceptional Handling

Uploaded by

Harene
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

SOFTWARE

SECURITY
EXCEPTIONAL HANDLING
Outlines

• Types of errors
• Exceptions
• Handling exceptions
• Raise statement
• Some common exceptions

2
ERRORS

Error is a abnormal condition whenever it occurs, the


execution of the program is stopped.

An error is a term used to describe any issue that


arises unexpectedly that causes a computer to not
function properly.

Computers can encounter either software errors or


hardware errors.
ERRORS

Errors are mainly classified into following types.

1. Syntax Errors
2. Semantic Errors
3. Run Time Errors
4. Logical Errors
w ro ng
iting is
Wr 1. Syntax Errors
There
is no
outpu
t yet

Syntax errors refer to formal rules governing the


construction of valid statements in a language.

Syntax errors occur when rules of a programming


language are misused i.e., when a grammatical rule
of the language is violated.
w ro ng
iting is
Wr 1. Syntax Errors
There
is no
outpu
t yet
For instance in the following program segment,
def main()
a=10
b=3
print(“ Sum is “, a+b ) Missing
g is
Writin 2. Semantic Errors
correct There
is no
outpu
Semantic errors occur when statements are not t yet
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.
g is
Writin 2. Semantic Errors
correct There
is no
outpu
See the following statement, t yet

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.
a×b=z
will result in a semantical error as an expression
can not come on the left side of an assignment
statement.
ron g
Referen
ce i s w 3. Run Time Errors
There
is no
outpu
t yet
• A Run time error occurs during execution of the
program. It is caused because of some illegal operation
taking place.

For example
1. If a program is trying to open a file which does not
exist or it could not be opened (meaning file is corrupted),
results into an error execution.
2. An expression is trying to divide a number by zero
are RUN TIME ERRORS.
is w r ong 4. Logical Errors
t
Outpu
At ou
tput
le vel
A logical error is that error which causes a program
to produce an incorrect or undesired output.

for instance,
ctr=1;
while(ctr<10):
print(n *ctr)
EXCEPTIONS
Exceptions

What is an exception?

Even if a statement or expression is


syntactically correct, it may cause an error
when an attempt is made to execute it.

Errors detected during execution are


called exceptions
Exceptions

For Example
HANDLING EXCEPTIONS
Handling Exceptions

• It is possible to write programs that handle


selected exceptions.

For Example
Handling Exceptions

The try statement works as follows.

First, the try clause (the statement(s) between the try


and except keywords) is executed.

If no exception occurs, the except clause is skipped


and execution of the try statement is finished.
Handling Exceptions

The try statement works as follows.

If an exception occurs during execution of the try


clause, the rest of the clause is skipped.

Then if its type matches the exception named after


the except keyword, the except clause is executed, and
then execution continues after the try statement.
Handling Exceptions

The try statement works as follows.

If its type does not match the exception named in the
except clause, it is passed on to outer try statement;
if no handler is found, it is an unhandled exception
and execution stops.
The except Clause with No Exceptions
The except Clause with No Exceptions

You can also use the except statement with no


exceptions defined as follows:
try:
You do your operations here;
except:
If there is any exception, then execute this
block…..
else:
If there is no exception then execute this block.
The except Clause with No Exceptions

This kind of a try-except statement catches all


the exceptions that occur.

Using this kind of try-except statement is not


considered a good programming practice though,
because it catches all exceptions but does not make
the programmer identify the root cause of the
problem that may occur.
RAISE STATEMENT
Raise statement

You can raise an exception in your


own program by using the raise
exception.

Raising an exception breaks current


code execution and returns the exception
back until it is handled.
Syntax:

raise [expression1[, expression2]]


Raise statement - Example

def check_age(age):
if age < 0: In this example, the check_age function
raises a ValueError if the given age is
raise ValueError(“Age cannot be negative”)
negative or less than 18. The try-except
elif age < 18: block catches this error and prints the
raise ValueError(“Must be at least 18 years old”) error message.
else:
print(“Age is valid”)
The code extracts try: check_age(-5) is
using a try block in Python to attempt to
try: execute the check_age(-5) function call
check_age(-5) within the block.
except ValueError as e: If any exceptions occur during the
print("Error occurred:", e) execution of this function call, they will
be caught by an associated except block to
handle the error.
SOME COMMON EXCEPTIONS
Some common exceptions

IOError
If the file cannot be opened.

ImportError
If python cannot find the module.

ValueError
Raised when a built-in operation or function receives
an argument that has the right type but an
inappropriate value.
Some common exceptions

KeyboardInterrupt
Raised when the user hits the interrupt key (normally
Control-C or Delete).

EOFError
Raised when one of the built-in functions (input() or
raw_input()) hits an end-of-file condition (EOF)
without reading any data
1. Explain the types of errors.

2. What is an exception? Explain in detail

3. What is raise? explain in detail.

4. Explain some of the common built in


exceptions provided in python.
CLASS TEST

2.
Exceptions are errors detected during execution.
When an exception occurs, the program execution is interrupted, and the
control is transferred to an exception handler.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy