D522: Error Handling & Exception Management in IT Automation
Domain 5: Error Handling and Exception Management
In IT automation, robust error handling ensures that a script does not crash when
encountering common network or file system issues (e.g., a server being offline or a
configuration file being missing).
The Try-Except Block Structure
Python uses try and except blocks to catch and handle exceptions gracefully.
● try: The block of code where exceptions may occur.
● except: The block of code that executes if an error is raised.
● else: Executes only if the try block was successful (no errors).
● finally: Executes no mafler what, often used for "cleanup" tasks like closing database
connections or files.
Common Built-in Exceptions in D522
Exception Description Automation Context
FileNotFoundError Raised when a file or Aflempting to read a
directory is requested but non-existent hosts.txt file.
doesn't exist.
PermissionError Raised when trying to run Aflempting to write a log
an operation without file to a restricted system
adequate access rights. directory.
ConnectionRefusedError Raised when a connection Running a port scanner
aflempt is rejected by the against a hardened server.
target host.
TimeoutError Raised when a system Waiting too long for a
function timed out at the network response.
system level.
KeyError Raised when a dictionary Accessing a specific JSON
key is not found. aflribute that is missing
from a response.
D522: Error Handling & Exception Management in IT Automation
Domain 5: Error Handling and Exception Management
In IT automation, robust error handling ensures that a script does not crash when
encountering common network or file system issues (e.g., a server being offline or a
configuration file being missing).
The Try-Except Block Structure
Python uses try and except blocks to catch and handle exceptions gracefully.
● try: The block of code where exceptions may occur.
● except: The block of code that executes if an error is raised.
● else: Executes only if the try block was successful (no errors).
● finally: Executes no mafler what, often used for "cleanup" tasks like closing database
connections or files.
Common Built-in Exceptions in D522
Exception Description Automation Context
FileNotFoundError Raised when a file or Aflempting to read a
directory is requested but non-existent hosts.txt file.
doesn't exist.
PermissionError Raised when trying to run Aflempting to write a log
an operation without file to a restricted system
adequate access rights. directory.
ConnectionRefusedError Raised when a connection Running a port scanner
aflempt is rejected by the against a hardened server.
target host.
TimeoutError Raised when a system Waiting too long for a
function timed out at the network response.
system level.
KeyError Raised when a dictionary Accessing a specific JSON
key is not found. aflribute that is missing
from a response.
D522: Error Handling & Exception Management in IT Automation