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

Chapter 6 (Exception Handling)

Uploaded by

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

Chapter 6 (Exception Handling)

Uploaded by

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

GD GOENKA INTERNATIONAL SCHOOL

NOIDA EXTENSION
CLASS 12
COMPUTER SCIENCE
CHAPTER 6. EXCEPTION HANDLING

• Contradictory or unexpected situation or unexpected error, during


program execution is known as Exception.
• There are two types of errors:
o Compile- time errors: They occur out of violation of programming
language’s grammar rules that is writing syntactically incorrect
statements.
o Run- time errors: They occur during runtime because of
unexpected situations.
Exception Handling: Way of handling anomalous situations in a program- run is
known as exception handling.
Advantages of exception handling are:
• Exception handling separates error- handling code from normal code.
• It clarifies the code by removing error- handling code from main line of
program and enhances readability.
• It stimulates consequences as the error- handling takes place at one
place and in one manner.
• It makes for clear, robust, fault- tolerant programs.
Some common examples of exceptions are:
• Divide by zero errors
• Accessing the elements of an array beyond its range
• Invalid input
• Hard disk crash
• Opening a non- existent file
• Heap memory exhausted
When to use Exception Handling?
• Processing exceptional situations
• Processing exceptions for components that cannot handle them directly.
• Large projects that require uniform error- processing.
Terminologies used within exception handling:
• Exception: An unexpected error that occurs during run time.
• Try Block: A set of code that might have an exception thrown in it.
• Throwing or raising an error: The process by which an exception is
generated and passed to the program.
• Catching: Capturing an exception that has just occurred and executing
statements that try to resolve the problem.
• Except clause or except/ exception block or catch block: The block of
code that attempts to deal with the exception.
• Stack trace: The sequence of method calls that brought control to the
point where the exception occurred.
Exception Handling in Python: Exception handling in Python involves the use
of try and except clause in the following format wherein the code that may
generate an exception is written in the try block and the code for handling
exception when the exception is raised, is written in except block.

For coding part, kindly refer page number 262, page number 263, Examples 6.2
and 6.3.
For the descriptions of some built- in Python exceptions, refer table given on
Page Number 263.

The finally block: One can use a finally block along with a try block, just like
you use except block. Refer heading 6.4.4 on page 266 for its coding
explanation.

The difference between an except block and the finally block is that the finally
block is a place that contains any code that must execute, whether the try
block raised an exception or not.
One can combine finally with except clause. In such cases, the except block will
get executed only in case an exception is raised and finally block will get
executed ALWAYS in the end. Refer heading 6.4.4 on page 266 for its coding
explanation.

Raising/ Forcing an Exception:


• raise keyword is used to raise/ force an exception that means, one can
force an exception to occur through raise keyword. It can also pass a
custom message to your exception handling module.
raise <exception> (message)
The exception raised in this way should be a pre- defined Built- in
exception. Refer heading 6.4.5 on page 267 for its coding explanation.
• The assert keyword in Python is used when we need to detect problems
early. Python’s assert statement is a debugging aid that tests a condition.
If the condition is true, it does nothing and the program just continues to
execute. But if the assert condition evaluates to false, it raises an
AssertionError exception with an optional error message. The syntax of
assert statement in Python is: assert condition [, error_message]
Refer heading Python assert statement Note Box on page 267 for its
coding explanation.

You might also like