0% found this document useful (0 votes)
13 views35 pages

Chapter 16

Chapter 16 covers errors and exceptions in programming, detailing types of errors such as semantic, logical, and runtime errors. It explains exception handling techniques, including try-except clauses, and the benefits of managing exceptions for better user experience and application reliability. The chapter also discusses built-in and user-defined exceptions, along with the importance of clean-up actions in code execution.

Uploaded by

ViShesh
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)
13 views35 pages

Chapter 16

Chapter 16 covers errors and exceptions in programming, detailing types of errors such as semantic, logical, and runtime errors. It explains exception handling techniques, including try-except clauses, and the benefits of managing exceptions for better user experience and application reliability. The chapter also discusses built-in and user-defined exceptions, along with the importance of clean-up actions in code execution.

Uploaded by

ViShesh
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/ 35

Chapter 16

ERRORS AND EXCEPTIONS


CHAPTER
1

Chapter 16.Errors and Exceptions


Learning Objectives
After completing this chapter, the reader will be familiar with the
following concepts:

• Understand errors and exceptions.

• Understand the standard exceptions

• Understand how exception-handling aids to handle run-time


exceptions.
• Learn to extract information from exceptions, raise an exception finally
statement, and catch exceptions in programs.

• Learn to handle exceptions such as using except clauses with no exceptions,


except clauses to specify exception handlers, and using except clause with
multiple exceptions.

• To try-finally clause—learn to use the try statement to delimit the code in


which exceptions may occur and use the final clause to release resources.
• Understand the argument of an exception and raise an exception.

• Understand the user-defined exceptions.

• To create program defined exceptions.


16.1 Introduction to Errors and
Exceptions
1. Anomalies

Fig. 16.1: Classification of Anomalies


2. Errors
Types of Errors

Fig. 16.2: Types of Errors


• Semantic errors: the semantic error occurs when the program's
semantics (meaning) is erroneous, or the statements have no meaning

• Logical errors: Logical errors are difficult to trace since they are
caused by faulty logical implementation and might happen at any time.
Table 16.1: Correct Logic Versus Logical Errors
Runtime errors: "Runtime errors" occur during an application's
execution.

Fig. 16.3: Compile Time Errors and Runtime Errors


Other Types of Errors

• Type errors

• ValueError

• ZeroDivisionError

• FileNotFoundError

• NotImplementedError
Table 16.2: Difference between Syntax Errors and Logic Errors
3. Exception
• Exceptional conditions: When a system goes through something that is not
typical of the system's behaviour, extenuating factors can be accused of the
event.

• Exception failures: two-thirds of all system breakdowns and 60 per cent of


all security holes in computer systems are attributed to exception failures.
Table 16.3: The Difference between an Error and an Exception
16.2 Handling Exceptions

Fig. 16.5: Handling Exceptions


Rule of Exceptions

• If an exception arises but is not always catastrophic, it can be handled.

• The exception clause in a try statement should include a pointer to a


specific class.

• A specific object type must exist for an exception to apply.


Listed below are a few examples of an exception.

• An exception based on arithmetic.

• Attempting to access a file that cannot possibly exist.

• Exception to the standard number format.

• If the datatype server is inaccessible, the table can be deleted.

• The combination of two distinct yet incompatible kinds.

• The index of the array is outside the bounds.

• Zero cannot be divided by a number.


Advantages of Handling Exceptions

• The advantages of Handling Exceptions are as follows.

• Decoupling the "Regular" code from the error-handling code.

• Aids in grouping and differentiating error types.

• Sending errors up the call stack when they occur.

• To correct errors that occur during the runtime.

• Use events to communicate the right circumstances without


transferring results all over a program by using the notify function.
Benefits of Throwing Exceptions

Throwing and handling the exceptions benefits us from the following.

• Better user experience.

• More helpful error messages for everyone: from users to fellow


developers.

• Reliability: to prevent the application from crashing suddenly by


avoiding potential problems in advance and taking preventative
measures.
Fig. 16.6: The Sequence of Events in Handling Exceptions
try - except - else
16.3 Multiple Exceptions
Exceptions can be handled in two ways with Python.
16.4 Raising Exceptions
16.6 Built-In Exceptions

Types of Exceptions

Python supports two diverse kinds of exceptions (Fig. 18.11).

• Built-in exceptions

• User-defined exceptions

Fig. 16.7: Types of Exceptions


Fig. 16.8: Most Relevant Exception Classes
Table 16.4: Base Class Exception Name
Table 16.5: Predefined Exception Classes Name
Table 16.6: Python Built-In Exceptions and Their Classes
16.8 Clean-Up Actions

• Code can have three distinct scenarios as follows.

• Code normally runs:

• Error handling is done in the except clause:

• Error without any except clause:


Try-Finally Clause

Fig. 16.9: Try Finally Clause

You might also like