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

Errors and Exceptions

Uploaded by

adilkhalili9999
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)
4 views

Errors and Exceptions

Uploaded by

adilkhalili9999
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/ 14

Select Topics in Python

Errors and Exceptions


Objectives

Objectives:
• Discover the types of errors.
• Use exceptions.
Types of Error

| Errors
- Syntax error.
- Semantics error.
- Run time error.
- Logical error.
Types of Error

| Syntax error
- Syntax errors: refers 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.
Types of Error

| Semantics errors
- Semantics errors occur when statements are not meaningful.
- Semantics refers to the set of rules which give the meaning of the
statement.
- Examples:
• Jon is registered for a class.
• It is semantically and syntactically correct.
• Class registered a class for Jon.
• Incorrect and not Semantically correct.
Types of Error

| Run time errors


- A run time error occurs during the execution of a program.
It is caused when an illegal operation has taken place.
- As an example:
• Open/write to a file.
• Divide by zero.
Types of Error

| Logical errors
- A logical error causes a program to produce an
incorrect or undesired output.
- As an example:
• Infinite loops
• Mathematical operation.
Exceptions

| What is an exception?
- Even when a statement or
expression is syntactically correct,
it may cause an error when an
attempt is made to execute it.
- An error during this process is
called an exception.
Handling Exceptions

| It is possible to write programs


that handle selected exceptions.
Look at the following example,
which asks the user for input
until a valid integer has been
entered, but allows the user to
interrupt the program using
Control-C or whatever the
operating system supports. Note
that a user-generated
interruption is signalled by
raising the KeyboardInterrupt
exception.
Exception Clause with No Excepptions

| This kind of a try-except


statement catches all the
exceptions that occur. Using this
kind of try-catch statement is not
considered a good programming
practice, because but catches all
exceptions butt doesn’t make the
programmer identify the root
cause of the problem that
occurred.
Common Exceptions

| IOError
- Raised when the file cannot be opened.

| ImportError
- Raised when Python cannot find the module.

| ValueError
- Raised when a built-in operation or function received an argument that has the right
type but an inappropriate value.

| KeyboardInterrupt
- Raised when the user hits the interrupt key.

| EOFError
- Raised when one of the built-in functions(input() or raw_input()) hits an end-of-file
without reading data.
Reading File

| Errors
Write File

| Errors

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