Exceptions#
Exception classes for librosa
Librosa provides a structured exception hierarchy rooted in LibrosaError.
Exceptions raised by Librosa inherit from both LibrosaError and relevant Python standard library exception classes where appropriate, allowing callers to catch errors using either Librosa-specific classes or familiar built-in exception types.
Exception Hierarchy#
treeView-beta
LibrosaError
├── ParameterError
│ ├── ParameterValueError ## (also inherits ValueError)
│ │ └── ParameterShapeError
│ ├── ParameterTypeError ## (also inherits TypeError)
│ └── ParameterKeyError ## (also inherits KeyError)
├── StreamError
│ ├── StreamClosedError ## (also inherits RuntimeError)
│ ├── StreamBufferError ## (also inherits BufferError)
│ └── StreamTimeoutError ## (also inherits TimeoutError, queue.Empty)
└── LibrosaNotImplementedError ## (also inherits NotImplementedError)
Usage Guidance#
Catching specific validation failures: Use specific parameter exception classes to respond to targeted errors:
try: D = librosa.stft(y, n_fft=n_fft, hop_length=hop_length) except librosa.ParameterShapeError: # Handle array shape, dimensionality, or channel mismatch ... except librosa.ParameterValueError: # Handle out-of-range numerical parameters or invalid options ... except librosa.ParameterTypeError: # Handle incorrect argument types ...
Interoperability with standard library exceptions: Because specific parameter exceptions inherit from standard built-ins (
ValueError,TypeError,KeyError), existing exception handlers in user code will catch them naturally:try: chroma = librosa.feature.chroma_stft(y=y, sr=sr) except ValueError: # Catches both ParameterValueError and ParameterShapeError ...
Catching all parameter validation errors: Catch
ParameterErrorto intercept any parameter or input validation error regardless of subtype. Existing code usingexcept librosa.ParameterError:continues to work unchanged.Catching any Librosa error: Catch
LibrosaErroras a top-level catch-all for any exception originating within Librosa.Upstream and backend errors: Exceptions originating in underlying backend libraries (such as
soundfile,pooch, or network errors fromrequests) are allowed to propagate directly without interception.
The root librosa exception class |
|
Base exception class for mal-formed inputs or parameter validation errors |
|
Exception class for invalid parameter values |
|
Exception class for parameter type mismatches |
|
Exception class for array shape, dimension, or channel mismatches |
|
Exception class for invalid lookup keys, catalog entries, or note/interval names |
|
Base exception class for streaming runtime errors |
|
Exception raised when an operation is attempted on a closed stream bridge |
|
Exception raised when a stream buffer overflows under the error policy |
|
Exception raised when waiting for a stream buffer chunk times out |
|
Exception class for abstract or unimplemented interface methods |