class DocumentNotFound
A custom exception class that is raised when a requested document cannot be found in the system.
/tf/active/vicechatdev/rmcl/exceptions.py
10 - 13
simple
Purpose
This exception class is designed to provide a specific error type for document retrieval failures. It extends Python's built-in Exception class to create a domain-specific exception that can be caught and handled separately from generic exceptions. This allows for more precise error handling in document management systems, databases, or file storage operations where distinguishing between 'document not found' and other errors is important.
Source Code
class DocumentNotFound(Exception):
"""Could not found a requested document"""
def __init__(self, msg):
super(DocumentNotFound, self).__init__(msg)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
Exception | - |
Parameter Details
msg: A string message that describes the specific document not found error. This message should provide context about which document was requested and why it couldn't be found. It will be passed to the parent Exception class and displayed when the exception is raised or printed.
Return Value
Instantiation returns a DocumentNotFound exception object that can be raised using the 'raise' keyword. When raised, it behaves like any Python exception and can be caught in try-except blocks. The exception object contains the error message passed during initialization.
Class Interface
Methods
__init__(self, msg: str) -> None
Purpose: Initializes the DocumentNotFound exception with a custom error message
Parameters:
msg: A string describing the document not found error, including context about which document was requested
Returns: None - this is a constructor that initializes the exception instance
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
args |
tuple | Inherited from Exception base class. Contains the error message as a tuple element. Automatically set by the parent Exception class. | instance |
Usage Example
# Example 1: Raising the exception
def get_document(doc_id):
documents = {'doc1': 'content1', 'doc2': 'content2'}
if doc_id not in documents:
raise DocumentNotFound(f"Document with ID '{doc_id}' was not found in the database")
return documents[doc_id]
# Example 2: Catching the exception
try:
document = get_document('doc3')
except DocumentNotFound as e:
print(f"Error: {e}")
# Handle the missing document case
document = None
# Example 3: Using in a class method
class DocumentRepository:
def __init__(self):
self.documents = {}
def retrieve(self, doc_id):
if doc_id not in self.documents:
raise DocumentNotFound(f"Document '{doc_id}' does not exist in repository")
return self.documents[doc_id]
Best Practices
- Always provide a descriptive error message when raising this exception to help with debugging and error logging
- Use this exception specifically for document-not-found scenarios rather than generic file or data access errors
- Catch this exception specifically in try-except blocks when you need to handle missing documents differently from other errors
- Consider including the document identifier (ID, name, path) in the error message for better traceability
- This exception should be raised at the point where the document lookup fails, not propagated from lower-level exceptions
- Document in your API/module documentation that this exception can be raised by document retrieval methods
- Consider logging the exception before re-raising it if you need audit trails of failed document access attempts
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class ResourceNotFoundError_v1 81.5% similar
-
class ResourceNotFoundError 73.4% similar
-
class FolderNotFound 72.4% similar
-
class DocumentProcessingError 71.4% similar
-
class ApiError 68.8% similar