Errors and Exceptions
Errors and Exceptions
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
| 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
| 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