-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
57 lines (40 loc) · 1.48 KB
/
exceptions.py
File metadata and controls
57 lines (40 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Custom exceptions for pystl data structures.
This module defines all custom exceptions used throughout the package
to provide clear error messages matching C++ STL behavior.
"""
class PySTLException(Exception):
"""Base exception class for all pystl exceptions."""
pass
class EmptyContainerError(PySTLException):
"""
Exception raised when attempting to access elements from an empty container.
This matches the behavior of C++ STL when accessing empty containers
(e.g., calling top() on an empty stack).
"""
def __init__(self, container_type: str):
self.container_type = container_type
super().__init__(f"Cannot access element from empty {container_type}")
class OutOfRangeError(PySTLException):
"""
Exception raised when accessing an invalid index or position.
This matches C++ STL's std::out_of_range exception.
"""
def __init__(self, index: int, size: int):
self.index = index
self.size = size
super().__init__(f"Index {index} is out of range for container of size {size}")
class KeyNotFoundError(PySTLException):
"""
Exception raised when a key is not found in an associative container.
This matches C++ STL behavior when accessing non-existent keys in maps.
"""
def __init__(self, key):
self.key = key
super().__init__(f"Key '{key}' not found in container")
__all__ = [
'PySTLException',
'EmptyContainerError',
'OutOfRangeError',
'KeyNotFoundError'
]