SlideShare a Scribd company logo
1 of 38
CHAPTER 13
EXCEPTIONAL HANDLING
Unit 2:
Computational Thinking and Programming
XI
Computer Science (083)
Board : CBSE
Courtesy CBSE
Unit II
Computational Thinking and Programming
(60 Theory periods 45 Practical Periods)
DCSc & Engg, PGDCA,ADCA,MCA.MSc(IT),Mtech(IT),MPhil (Comp. Sci)
Department of Computer Science, Sainik School Amaravathinagar
Cell No: 9431453730
Praveen M Jigajinni
Prepared by
Courtesy CBSE
CHAPTER 13
EXCEPTIONAL HANDLING
ERRORS
Error is a abnormal condition
whenever it occurs execution of the
program is stopped.
An error is a term used to describe
any issue that arises unexpectedly that
cause 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. Type of Errors
4. Run Time Errors.
5. Logical Errors.
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.
1. Syntax Errors
For instance in the following program
segment,
def main()
a=10
b=3
print(“ Sum is “,a+b
1. Syntax Errors
: (Colon) Missing
) Missing
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 an
expression can not come on the left side of
an assignment statement.
2. Semantic Errors
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 type error.
3. Type of Errors
3. Type of Errors
A Run time error is that 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 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.
4. Run Time 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;
5. Logical Errors.
Exceptions
Exceptions
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
What is an exception?
Exceptions
For Example
Handling Exceptions
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.
Handling 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 an exception occurs which does not
match the exception named in the except
clause, it is passed on to outer try
statements; if no handler is found, it is an
unhandled exception and execution stops
with a message as shown above.
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. Contd..
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.
Example
Example
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 Example
raise Example
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
CLASS TEST
CLASS TEST
Time: 40 Min Max Marks: 20
1. Explain the types of errors 05
2. What is an exception? Explain in detail 05
3. What is raise? explain in detail 05
4. Explain some of the common built in
exceptions provided in python 05
Thank You

More Related Content

What's hot

What's hot (20)

Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Exception Handling in C++
Exception Handling in C++Exception Handling in C++
Exception Handling in C++
 
How to execute a C program
How to execute a C  program How to execute a C  program
How to execute a C program
 
C functions
C functionsC functions
C functions
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Exceptions handling notes in JAVA
Exceptions handling notes in JAVAExceptions handling notes in JAVA
Exceptions handling notes in JAVA
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Data types in java
Data types in javaData types in java
Data types in java
 
Operators and Control Statements in Python
Operators and Control Statements in PythonOperators and Control Statements in Python
Operators and Control Statements in Python
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
Function in C program
Function in C programFunction in C program
Function in C program
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
 
Exception handling
Exception handlingException handling
Exception handling
 
Introduction to C++
Introduction to C++ Introduction to C++
Introduction to C++
 

Similar to Chapter 13 exceptional handling

Exception handling
Exception handlingException handling
Exception handlingRaja Sekhar
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaKavitha713564
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overviewBharath K
 
Types of Exceptionsfsfsfsdfsdfdsfsfsdfsd
Types of ExceptionsfsfsfsdfsdfdsfsfsdfsdTypes of Exceptionsfsfsfsdfsdfdsfsfsdfsd
Types of Exceptionsfsfsfsdfsdfdsfsfsdfsdssuserce9e9d
 
Class notes(week 8) on exception handling
Class notes(week 8) on exception handlingClass notes(week 8) on exception handling
Class notes(week 8) on exception handlingKuntal Bhowmick
 
Lecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxLecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxVishuSaini22
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handlingAlpesh Oza
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handlingAlpesh Oza
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handlingAlpesh Oza
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception HandlingMaqdamYasir
 

Similar to Chapter 13 exceptional handling (20)

Unit 5 Java
Unit 5 JavaUnit 5 Java
Unit 5 Java
 
Exception handling
Exception handlingException handling
Exception handling
 
Z blue exception
Z blue   exceptionZ blue   exception
Z blue exception
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
Types of Exceptionsfsfsfsdfsdfdsfsfsdfsd
Types of ExceptionsfsfsfsdfsdfdsfsfsdfsdTypes of Exceptionsfsfsfsdfsdfdsfsfsdfsd
Types of Exceptionsfsfsfsdfsdfdsfsfsdfsd
 
6-Error Handling.pptx
6-Error Handling.pptx6-Error Handling.pptx
6-Error Handling.pptx
 
Event handling
Event handlingEvent handling
Event handling
 
Class notes(week 8) on exception handling
Class notes(week 8) on exception handlingClass notes(week 8) on exception handling
Class notes(week 8) on exception handling
 
Presentation1
Presentation1Presentation1
Presentation1
 
Lecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptxLecture 1 Try Throw Catch.pptx
Lecture 1 Try Throw Catch.pptx
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception Handling
 
How to handle exceptions in Java Technology
How to handle exceptions in Java Technology How to handle exceptions in Java Technology
How to handle exceptions in Java Technology
 
$Cash
$Cash$Cash
$Cash
 
$Cash
$Cash$Cash
$Cash
 
Introduction to Exception
Introduction to ExceptionIntroduction to Exception
Introduction to Exception
 

More from Praveen M Jigajinni

Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithmsPraveen M Jigajinni
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructorsPraveen M Jigajinni
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programmingPraveen M Jigajinni
 
Chapter 8 getting started with python
Chapter 8 getting started with pythonChapter 8 getting started with python
Chapter 8 getting started with pythonPraveen M Jigajinni
 
Chapter 7 basics of computational thinking
Chapter 7 basics of computational thinkingChapter 7 basics of computational thinking
Chapter 7 basics of computational thinkingPraveen M Jigajinni
 
Chapter 6 algorithms and flow charts
Chapter 6  algorithms and flow chartsChapter 6  algorithms and flow charts
Chapter 6 algorithms and flow chartsPraveen M Jigajinni
 
Chapter 3 cloud computing and intro parrallel computing
Chapter 3 cloud computing and intro parrallel computingChapter 3 cloud computing and intro parrallel computing
Chapter 3 cloud computing and intro parrallel computingPraveen M Jigajinni
 

More from Praveen M Jigajinni (20)

Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithms
 
Chapter 08 data file handling
Chapter 08 data file handlingChapter 08 data file handling
Chapter 08 data file handling
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Chapter 06 constructors and destructors
Chapter 06 constructors and destructorsChapter 06 constructors and destructors
Chapter 06 constructors and destructors
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programming
 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Unit 3 MongDB
Unit 3 MongDBUnit 3 MongDB
Unit 3 MongDB
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
 
Chapter 10 data handling
Chapter 10 data handlingChapter 10 data handling
Chapter 10 data handling
 
Chapter 9 python fundamentals
Chapter 9 python fundamentalsChapter 9 python fundamentals
Chapter 9 python fundamentals
 
Chapter 8 getting started with python
Chapter 8 getting started with pythonChapter 8 getting started with python
Chapter 8 getting started with python
 
Chapter 7 basics of computational thinking
Chapter 7 basics of computational thinkingChapter 7 basics of computational thinking
Chapter 7 basics of computational thinking
 
Chapter 6 algorithms and flow charts
Chapter 6  algorithms and flow chartsChapter 6  algorithms and flow charts
Chapter 6 algorithms and flow charts
 
Chapter 5 boolean algebra
Chapter 5 boolean algebraChapter 5 boolean algebra
Chapter 5 boolean algebra
 
Chapter 4 number system
Chapter 4 number systemChapter 4 number system
Chapter 4 number system
 
Chapter 3 cloud computing and intro parrallel computing
Chapter 3 cloud computing and intro parrallel computingChapter 3 cloud computing and intro parrallel computing
Chapter 3 cloud computing and intro parrallel computing
 

Recently uploaded

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 

Chapter 13 exceptional handling

  • 2. Unit 2: Computational Thinking and Programming XI Computer Science (083) Board : CBSE Courtesy CBSE
  • 3. Unit II Computational Thinking and Programming (60 Theory periods 45 Practical Periods) DCSc & Engg, PGDCA,ADCA,MCA.MSc(IT),Mtech(IT),MPhil (Comp. Sci) Department of Computer Science, Sainik School Amaravathinagar Cell No: 9431453730 Praveen M Jigajinni Prepared by Courtesy CBSE
  • 5. ERRORS Error is a abnormal condition whenever it occurs execution of the program is stopped. An error is a term used to describe any issue that arises unexpectedly that cause a computer to not function properly. Computers can encounter either software errors or hardware errors.
  • 6. ERRORS Errors are mainly classified into following types. • 1. Syntax Errors 2. Semantic Errors 3. Type of Errors 4. Run Time Errors. 5. Logical Errors.
  • 7. 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. 1. Syntax Errors
  • 8. For instance in the following program segment, def main() a=10 b=3 print(“ Sum is “,a+b 1. Syntax Errors : (Colon) Missing ) Missing
  • 9. 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
  • 10. 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 an expression can not come on the left side of an assignment statement. 2. Semantic Errors
  • 11. 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 type error. 3. Type of Errors
  • 12. 3. Type of Errors
  • 13. A Run time error is that 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 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. 4. Run Time Errors.
  • 14. • 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; 5. Logical Errors.
  • 16. Exceptions 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 What is an exception?
  • 19. 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.
  • 21. 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.
  • 22. 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.
  • 23. Handling Exceptions The try statement works as follows. If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with a message as shown above.
  • 24. The except Clause with No Exceptions
  • 25. 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. Contd..
  • 26. 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.
  • 30. 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]]
  • 34. 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.
  • 35. 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
  • 37. CLASS TEST Time: 40 Min Max Marks: 20 1. Explain the types of errors 05 2. What is an exception? Explain in detail 05 3. What is raise? explain in detail 05 4. Explain some of the common built in exceptions provided in python 05