Exceptions in Computer Science

 
CHAPTER 10
CHAPTER 10
EXCEPTIONAL HANDLING
EXCEPTIONAL HANDLING
 
Unit 2:
Unit 2:
 
Computational Thinking and Programming
Computational Thinking and Programming
 
Courtesy  CBSE
Courtesy  CBSE
 
CHAPTER 10
CHAPTER 10
EXCEPTIONAL HANDLING
EXCEPTIONAL HANDLING
 
  
  
Error is a abnormal condition
Error is a abnormal condition
whenever it occurs execution of the
whenever it occurs execution of the
program is stopped.
program is stopped.
 
 
 
An error is a term used to describe
An error is a term used to describe
any issue that arises unexpectedly that
any issue that arises unexpectedly that
cause a computer to not function properly.
cause a computer to not function properly.
Computers can encounter either
Computers can encounter either
software errors or hardware errors.
software errors or hardware errors.
 
Errors are mainly classified into following types.
Errors are mainly classified into following types.
 
  
  
Syntax errors refer to formal
Syntax errors refer to formal
rules governing the construction of valid
rules governing the construction of valid
statements in a language.
statements in a language.
 
  
  
Syntax errors occur when rules
Syntax errors occur when rules
of a programming language are misused
of a programming language are misused
i.e., when a grammatical  rule of the
i.e., when a grammatical  rule of the
language is violated.
language is violated.
 
  
  
For instance in the following program
For instance in the following program
segment,
segment,
 
 
def main()
def main()
 
 
 
 
 
 
a=10
a=10
  
  
b=3
b=3
  
  
print(“ Sum is “,a+b
print(“ Sum is “,a+b
 
 
 
Semantics error 
Semantics error 
occur when statements
occur when statements
are not meaningful
are not meaningful
 
 
Semantics
Semantics
 refers to the set of rules
 refers to the set of rules
which give the meaning of the
which give the meaning of the
statement.
statement.
For example,
For example,
  
  
Rama plays Guitar
Rama plays Guitar
   
   
This statement is syntactically
This statement is syntactically
and semantically correct and it has
and semantically correct and it has
some meaning.
some meaning.
 
See the following statement,
See the following statement,
Guitar plays Rama
Guitar plays Rama
 
 
is syntactically correct (syntax is correct) but
is syntactically correct (syntax is correct) but
semantically incorrect. Similarly, there are
semantically incorrect. Similarly, there are
semantics rules of programming language,
semantics rules of programming language,
violation of which results in semantical
violation of which results in semantical
errors.
errors.
    
    
X * Y = Z
X * Y = Z
 
 
will result in semantical error as an
will result in semantical error as an
expression can not come on the left side of
expression can not come on the left side of
an assignment statement.
an assignment statement.
 
 
 
A Run time error is that occurs during
A Run time error is that occurs during
execution of the program. It is caused
execution of the program. It is caused
because of some illegal operation taking
because of some illegal operation taking
place.
place.
 
 
For example
For example
 
 
1. If a program is trying to open a file which
1. If a program is trying to open a file which
does not exists or it could not be
does not exists or it could not be
opened(meaning file is corrupted), results
opened(meaning file is corrupted), results
into an execution error.
into an execution error.
 
 
 
 
2. An expression is trying to divide a number
2. An expression is trying to divide a number
by zero are 
by zero are 
RUN TIME ERRORS.
RUN TIME ERRORS.
 
A Logical Error is that error which is
A Logical Error is that error which is
causes a program to produce incorrect
causes a program to produce incorrect
or undesired output.
or undesired output.
 
 
for instance,
for instance,
 
 
ctr=1;
ctr=1;
 
 
while(ctr<10):
while(ctr<10):
  
  
print(n *ctr
print(n *ctr
)
)
 
 
 
Even if a statement or expression is
Even if a statement or expression is
syntactically correct, it may cause an error
syntactically correct, it may cause an error
when an attempt is made to execute it.
when an attempt is made to execute it.
Errors detected during execution are
Errors detected during execution are
called 
called 
exceptions
exceptions
 
 
 
What is an exception?
What is an exception?
 
 
 
For Example
For Example
 
 
 
It is possible to write programs that
It is possible to write programs that
handle selected exceptions. Look at the
handle selected exceptions. Look at the
following example, which asks the user for
following example, which asks the user for
input until a valid integer has been
input until a valid integer has been
entered, but allows the user to interrupt
entered, but allows the user to interrupt
the program (using Control-C or whatever
the program (using Control-C or whatever
the operating system supports); note that
the operating system supports); note that
a user-generated interruption is signalled
a user-generated interruption is signalled
by raising the KeyboardInterrupt
by raising the KeyboardInterrupt
exception.
exception.
 
 
 
For Example
For Example
 
 
 
 
 
The try statement works as follows.
The try statement works as follows.
 
First, the try clause (the statement(s)
First, the try clause (the statement(s)
between the try and except keywords) is
between the try and except keywords) is
executed.
executed.
 
If no exception occurs, the except clause is
If no exception occurs, the except clause is
skipped and execution of the try statement
skipped and execution of the try statement
is finished.
is finished.
 
 
 
 
 
The try statement works as follows.
The try statement works as follows.
 
If an exception occurs during execution of
If an exception occurs during execution of
the try clause, the rest of the clause is
the try clause, the rest of the clause is
skipped. Then if its type matches the
skipped. Then if its type matches the
exception named after the except keyword,
exception named after the except keyword,
the except clause is executed, and then
the except clause is executed, and then
execution continues after the try statement.
execution continues after the try statement.
 
 
 
 
 
The try statement works as follows.
The try statement works as follows.
 
If an exception occurs which does not
If an exception occurs which does not
match the exception named in the except
match the exception named in the except
clause, it is passed on to outer try
clause, it is passed on to outer try
statements; if no handler is found, it is an
statements; if no handler is found, it is an
unhandled exception and execution stops
unhandled exception and execution stops
with a message as shown above.
with a message as shown above.
 
 
 
You can also use the except
You can also use the except
statement with no exceptions defined as
statement with no exceptions defined as
follows:
follows:
    try:
    try:
 
 
You do your operations here;
You do your operations here;
    except:
    except:
 
 
If there is any exception, then
If there is any exception, then
 
 
execute this block…..
execute this block…..
    else:
    else:
 
 
If there is no exception then execute
If there is no exception then execute
 
 
this block.                                    
this block.                                    
Contd..
Contd..
 
 
 
 
This kind of a 
This kind of a 
try-except
try-except
 statement
 statement
catches all the exceptions that occur. Using
catches all the exceptions that occur. Using
this kind of try-except statement is 
this kind of try-except statement is 
not
not
considered a good programming practice
considered a good programming practice
though, because it catches all exceptions
though, because it catches all exceptions
but does not make the programmer
but does not make the programmer
identify the root cause of the problem that
identify the root cause of the problem that
may occur.
may occur.
 
 
 
You can raise an exception in your own
You can raise an exception in your own
program by using the raise exception.
program by using the raise exception.
 
 
Raising an exception breaks current
Raising an exception breaks current
code execution and returns the exception
code execution and returns the exception
back until it is handled.
back until it is handled.
Syntax:
Syntax:
 
raise [expression1[, expression2]]
raise [expression1[, expression2]]
 
IOError
IOError
If the file cannot be opened.
If the file cannot be opened.
 
ImportError
ImportError
If python cannot find the module.
If python cannot find the module.
 
ValueError
ValueError
Raised when a built-in operation or function
Raised when a built-in operation or function
receives an argument that has the right type but
receives an argument that has the right type but
an inappropriate value.
an inappropriate value.
 
 
KeyboardInterrupt
KeyboardInterrupt
Raised when the user hits the interrupt key
Raised when the user hits the interrupt key
(normally Control-C or Delete).
(normally Control-C or Delete).
 
EOFError
EOFError
Raised when one of the built-in functions (input()
Raised when one of the built-in functions (input()
or raw_input()) hits an end-of-file condition (EOF)
or raw_input()) hits an end-of-file condition (EOF)
without reading any data
without reading any data
 
Time: 40 Min
Time: 40 Min
   
   
      Max Marks: 20
      Max Marks: 20
 
1.
Explain the types of errors
Explain the types of errors
   
   
       05
       05
 
2.   What is an exception? Explain in detail          05
2.   What is an exception? Explain in detail          05
 
3.   What is raise? explain in detail 
3.   What is raise? explain in detail 
  
  
       05
       05
 
4. Explain some of the common built in
4. Explain some of the common built in
exceptions provided in python
exceptions provided in python
  
  
       05
       05
 
Thank You
Thank You
Slide Note
Embed
Share

Errors in programming, such as syntax, semantic, runtime, and logical errors, can disrupt the execution of a program. Syntax errors relate to grammatical violations, semantic errors occur when statements lack meaning, and runtime errors happen during program execution due to illegal operations. By identifying and handling these errors effectively, programmers can ensure the proper functioning of their applications.

  • Programming
  • Computer Science
  • Errors
  • Exception Handling

Uploaded on Jul 20, 2024 | 2 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. CHAPTER 10 EXCEPTIONAL HANDLING

  2. XI Computer Science (083) Board : CBSE Unit 2: Computational Thinking and Programming Courtesy CBSE

  3. CHAPTER 10 EXCEPTIONAL HANDLING

  4. ERRORS Error is a abnormal condition whenever it occurs execution of the program is stopped. any issue that arises unexpectedly that cause a computer to not function properly. Computers can software errors or hardware errors. An error is a term used to describe encounter either

  5. ERRORS Errors are mainly classified into following types. 1. Syntax Errors 2. Semantic Errors 3. Run Time Errors. 4. Logical Errors.

  6. 1. Syntax Errors Syntax errors refer to formal rules governing the construction of valid statements in a language. of a programming language are misused i.e., when a grammatical rule of the language is violated. Syntax errors occur when rules

  7. 1. Syntax Errors segment, def main() a=10 b=3 print( Sum is ,a+b For instance in the following program : (Colon) Missing ) Missing

  8. 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.

  9. 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.

  10. 3. Run Time 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.

  11. 4. 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)

  12. Exceptions

  13. 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

  14. Exceptions For Example

  15. HandlingExceptions

  16. HandlingExceptions 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 exception. KeyboardInterrupt

  17. HandlingExceptions For Example

  18. HandlingExceptions 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.

  19. HandlingExceptions 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.

  20. HandlingExceptions 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.

  21. The except Clause with No Exceptions

  22. 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..

  23. 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.

  24. Example

  25. Example

  26. raise statement

  27. raise statement You can raise an exception in your own program by using the raise exception. code execution and returns the exception back until it is handled. Syntax: Raising an exception breaks current raise [expression1[, expression2]]

  28. raise Example

  29. raise Example

  30. Some common exceptions

  31. 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.

  32. 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

  33. CLASS TEST

  34. 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

  35. Thank You

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#