Libmagicxx v10.0.3
A modern C++23 wrapper for libmagic — the library that powers the Unix file command.
Loading...
Searching...
No Matches
magic_exception.hpp File Reference

Exception hierarchy for the Magic file identification library. More...

#include <format>
#include <stdexcept>
#include <string>
Include dependency graph for magic_exception.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Recognition::MagicException
 Base exception class for all Magic-related errors. More...
class  Recognition::NullTracker
 Exception thrown when a shared ProgressTracker is null. More...
class  Recognition::EmptyPath
 Exception thrown when a path argument is empty. More...
class  Recognition::PathIsNotRegularFile
 Exception thrown when a path is expected to be a regular file but is not. More...
class  Recognition::PathIsNotDirectory
 Exception thrown when a path is expected to be a directory but is not. More...
class  Recognition::PathDoesNotExist
 Exception thrown when a specified path does not exist. More...
class  Recognition::FilesystemError
 Exception thrown when a filesystem operation fails. More...
class  Recognition::MagicIsClosed
 Exception thrown when an operation is attempted on a closed Magic instance. More...
class  Recognition::MagicOpenError
 Exception thrown when Magic::Open() fails. More...
class  Recognition::MagicLoadDatabaseFileError
 Exception thrown when Magic::LoadDatabaseFile() fails. More...
class  Recognition::MagicDatabaseNotLoaded
 Exception thrown when file identification is attempted without a loaded database. More...
class  Recognition::MagicIdentifyFileError
 Exception thrown when file type identification fails. More...
class  Recognition::MagicSetFlagsError
 Exception thrown when Magic::SetFlags() fails. More...
class  Recognition::MagicSetParameterError
 Exception thrown when Magic::SetParameter() fails. More...

Namespaces

namespace  Recognition
 Root namespace for the libmagicxx library.

Detailed Description

Exception hierarchy for the Magic file identification library.

This file defines the exception classes used by the Magic class to report errors. All exceptions derive from MagicException, which itself derives from std::runtime_error.

Exception Hierarchy

std::runtime_error
└── MagicException (base class for all Magic errors)
├── NullTracker - Progress tracker is null
├── EmptyPath - Path is empty
├── PathIsNotRegularFile - Path is not a regular file
├── PathIsNotDirectory - Path is not a directory
├── PathDoesNotExist - Path does not exist
├── FilesystemError - Filesystem operation failed
├── MagicIsClosed - Magic instance is closed
├── MagicOpenError - Failed to open Magic
├── MagicLoadDatabaseFileError - Failed to load database
├── MagicDatabaseNotLoaded - Database not loaded
├── MagicIdentifyFileError - Failed to identify file
├── MagicSetFlagsError - Failed to set flags
└── MagicSetParameterError - Failed to set parameter

Exception Handling Patterns

Catch specific exceptions for targeted error handling:

try {
auto type = magic.IdentifyFile("/nonexistent/file.txt");
} catch (const Recognition::PathDoesNotExist& e) {
std::cerr << "File not found: " << e.what() << '\n';
std::cerr << "Identification failed: " << e.what() << '\n';
} catch (const Recognition::MagicException& e) {
std::cerr << "Magic error: " << e.what() << '\n';
}
Base exception class for all Magic-related errors.
Definition magic_exception.hpp:102
Exception thrown when file type identification fails.
Definition magic_exception.hpp:426
Exception thrown when a specified path does not exist.
Definition magic_exception.hpp:245

Or use noexcept overloads to avoid exceptions entirely:

auto result = magic.IdentifyFile("/path/to/file", std::nothrow);
if (!result) {
std::cerr << "Error: " << result.error() << '\n';
}
See also
Magic