Concepts / Handling Multiple Exception Types

Handling Multiple Exception Types

An ungraceful exit occurs when an unhandled exception causes a program to crash and print a traceback instead of terminating in a controlled way.

  • Programming
Interactive lab

Try it: try / except / else / finally

How an exception jumps out of a try block to the first matching except clause, when else runs, and why finally always runs.

How it works

  1. Statements in try run until one raises an exception; the rest of try is skipped.
  2. Python checks the except clauses in order and runs the first one whose type matches.
  3. If nothing was raised, the else block runs instead.
  4. finally runs in every case.

Default run (9 steps): Call safe_divide('4'). … Return 25.0. Output was: Worked: 25.0 / Done.

Simplified: One fixed function; int() and / are modelled exactly (including how Python prints floats), but nothing is executed as Python.

Educational simulation

Loading the simulation…

When the Happy Path Breaks

A file-handling program often begins with a simple request: ask the user for a file name and read the file. The difficult part is that the program cannot predict every user action or file condition. The user might enter a name that does not exist, misspell a path, or press Enter without entering anything. The file might also have permission problems, be a directory instead of a file, or involve an encoding issue. Each possibility is a potential failure point.

The important design question is not whether something can go wrong. It is whether the program will handle the problem in a controlled way or allow Python's default behavior to stop the program.

Failure Points in File Handling

acceptable conditionfailure conditionStartRequest file nameUser inputmissing or misspelled nameOpen fileFile conditionsmissing, restricted,directory, encodingRead contentsProgram failure
At which steps can a user's action or a file condition cause the program to fail?
Potential failure pointWhat may happen
Non-existent fileThe requested file cannot be found.
Permission errorThe program may not be allowed to access the file.
Empty inputThe user may press Enter without typing a file name.
Directory instead of a fileThe supplied path may refer to a directory rather than a file.
Encoding issueThe file's encoding may create a reading problem.

Common failure points identified for file-handling programs.

These possibilities do not all represent the same situation. A missing file, an empty user response, and an encoding issue arise at different points or for different reasons. Thinking about them separately helps you decide what your program should do when each one occurs. This is the central idea behind handling multiple exception types: anticipate more than one kind of failure instead of designing only for the successful case.

Following an Ungraceful Exit

A missing file request

A program asks for a file name. The user enters missing_report.txt, but that file does not exist.

Request: The program asks the user to supply a file name.

Input: The user enters missing_report.txt.

Open attempt: The program tries to open the named file.

Exception: Because the file cannot be found, open() raises a FileNotFoundError.

Propagation: The exception moves up the call stack because no try-except block catches it.

Termination: Python stops executing the program and prints a traceback.

The program exits ungracefully: the unhandled exception causes a crash and traceback instead of a controlled termination.

askssupplies namefile not founduncaughtProgramRequest file nameopen()missing_report.txtFileNotFoundErrorTraceback
What path did the program follow, and where did execution stop when the error occurred?

An ungraceful exit occurs when an unhandled exception causes a program to crash and print a traceback instead of terminating in a controlled way.

The failure does not occur without a path. The program follows its instructions until the attempt to open the file raises an exception. Because no handler takes control, the exception propagates and execution stops.

Reading the Traceback

A traceback is Python's report of where and why execution failed. It reveals the exact file, line number, code, and exception type at the moment of failure. These details let you reconstruct the execution path and identify the point where error handling is needed.

Traceback detailQuestion it answers
FileWhich file contained the failing code?
Line numberWhere in that file did execution fail?
CodeWhat operation was being attempted?
Exception type and messageWhat kind of failure occurred, and what explanation did Python provide?

The main information a traceback provides.

Using traceback information

A traceback reports a failure while the program is opening missing_report.txt.

Locate the file: Use the reported file name to identify which program file contains the failing operation.

Locate the line: Use the line number to find the exact instruction that was running when the failure occurred.

Read the code: Inspect the operation on that line. In this case, it is the attempt to open a file.

Interpret the exception: FileNotFoundError identifies the failure as a file-not-found problem.

Plan handling: Use this information to anticipate the same failure when a user supplies a name that does not identify an existing file.

The traceback turns an unexplained crash into a specific failure point that can be considered during error-handling design.

Graceful Versus Ungraceful Control

not caughtPython default behaviorcaughtprogram respondscontrolled outcomeExceptionUnhandled exceptionTracebackCaught exceptionHandle problemControlledtermination
What happens next after an error in a graceful exit compared with an unhandled exception and traceback?
Failure situationWhat the program must recognizePossible result when unhandled
Missing fileThe requested file does not exist.Python stops and reports FileNotFoundError in a traceback.
Permission problemAccess to the file is not permitted.The program may exit according to Python's default error behavior.
Empty inputThe user supplied no file name.The program may reach a failure point based on how that input is used.
Directory instead of a fileThe supplied path identifies a directory.The program may fail while attempting file handling.
Encoding issueThe file's encoding creates a reading problem.The program may fail while reading the contents.

A graceful exit means that the program catches an exception, handles it appropriately, and either recovers or terminates in a controlled way. An ungraceful exit leaves the exception unhandled, so Python terminates the program and prints a traceback. The difference is who controls the response: your program or Python's default behavior.

Planning Before Handling

  1. List the actions the user can take, including entering a misspelled name or pressing Enter without typing anything.
  2. List the conditions that may affect the file operation, including a missing file, permission problem, directory path, or encoding issue.
  3. Identify the operation at which each problem could appear, such as supplying input, opening the file, or reading its contents.
  4. Use traceback information to connect a future failure to its file, line number, code, and exception type.
  5. Decide how the program should respond so the problem is handled rather than left to Python's default behavior.

This planning step moves error handling from reaction to design. Instead of waiting for a user to discover a crash, you consider likely failure points before writing the program. The goal is not to predict every imaginable problem. It is to recognize the common ways a file-handling program can fail and decide how the program should take control of those situations.

Mistakes to Avoid

  • Designing only for the successful file name.

    Users may enter a misspelled name, a path for a non-existent file, or no name at all.

    Fix: List likely user actions and file conditions before writing the error-handling plan.

  • Treating every file failure as the same problem.

    These are different failure conditions that can occur at different stages of file handling.

    Fix: Identify the particular failure point and the information the traceback provides about it.

  • Ignoring the traceback after a crash.

    The traceback identifies the file, line number, code, and exception type involved in the failure.

    Fix: Read those details to locate the failure and anticipate how the program should handle it.

  • Confusing a controlled termination with an unhandled crash.

    A graceful exit requires the program to catch and handle the exception; an ungraceful exit leaves the response to Python.

    Fix: Ask whether the program took control of the exception before termination.

Practice the Diagnosis

MEDIUM

Imagine a program asks for a file name. A user presses Enter without typing anything, and the program later exits with a traceback. Before changing the program, describe three things: the user's action, the likely failure point, and the traceback details you would inspect to understand the failure.

Hints
  • Start with the input step rather than assuming the failure occurred while reading the file.
  • Name the file, line number, code, and exception type as the traceback details to inspect.
  • Consider how the response would differ if the exception were caught and handled.
EASY

A traceback says that the program failed on the line that attempts to open missing_report.txt, and it identifies the exception as FileNotFoundError. Explain what this tells you about the execution path and what kind of failure the program should anticipate in the future.

Hints
  • The program reached the file-opening operation.
  • The file name supplied by the user did not identify an existing file.
  • Use the exception type to connect the failure to a specific handling plan.

Key Takeaways

  1. An ungraceful exit happens when an unhandled exception stops the program and produces a traceback.
  2. A graceful exit happens when the program catches the exception, handles the problem, and recovers or terminates in a controlled way.
  3. A traceback reveals the file, line number, code, and exception type involved in the failure.
  4. File-handling programs should anticipate missing files, permission errors, empty input, directories instead of files, and encoding issues.
  5. Planning for multiple failure points before coding helps the program respond deliberately instead of relying on Python's default crash behavior.

Key Takeaways

  • Anticipate more than the successful file-handling path.
  • Use traceback details to reconstruct where execution stopped and why.
  • Distinguish a program-controlled graceful exit from Python's default ungraceful exit.
  • Consider each likely failure condition separately before designing error handling.