ipaddress 1.1.0
Loading...
Searching...
No Matches
Error Handling

The library provides two ways to handle errors: through exceptions and through returned error codes. Which method to choose depends on the task and policy of your project (not everywhere it is possible to use exceptions; moreover, they may even be disabled in your build).

Exception Handling

All methods not marked noexcept can throw exceptions; there are only a few such methods. The documentation labels them all, and also indicates the types of exceptions that can be thrown.

These are mainly methods for parsing from a string and some operations with networks from the previous section.

#include <iostream>
using namespace ipaddress;
int main() {
try {
ipv4_address::parse("192.257.2.0");
} catch (const parse_error& exc) {
std::cout << exc.what() << std::endl; // octet 1 of address 192.257.2.0 exceeded 255
}
return 0;
}
Exception for errors encountered during IP address parsing.
Definition errors.hpp:203
The main include file for the ipaddress library.
Namespace dedicated to IP addresses and networks operations.
Definition base-v4.hpp:18

That's basically all you need to know about exception handling. The only thing I’ll clarify is that universal classes for addresses and networks (with union IPv4 and IPv6) first attempt to parse for IPv4, and then IPv6, which is why the error will contain information about the non-corect IPv6 (in case of exception).

Handling Error Codes

If you do not use exceptions, then for all methods that can throw exceptions there are alternative methods that return an error code.

#include <iostream>
using namespace ipaddress;
int main() {
auto ip = ipv4_address::parse("192.257.2.0", err);
if (err != error_code::no_error) {
std::cout << "invalid ip" << std::endl;
return 1;
}
std::cout << "ip " << ip << std::endl;
return 0;
}
error_code
Enumeration of error codes for IP address parsing and validation.
Definition errors.hpp:52

Disable Exceptions

Also, your compiler may have exceptions disabled. In this case, you can also disable exceptions in the library.

Note
The library supports the work without any exceptions, if necessary. To disable code that may throw exceptions, pass the IPADDRESS_NO_EXCEPTIONS definition when building.

After this, the library will not throw exceptions when errors occur. It is recommended in this case to use error codes to handle errors.

This is what the behavior will be if you pass this flag.

#include <iostream>
using namespace ipaddress;
int main() {
// try {
auto empty = ipv4_address::parse("192.257.2.0");
std::cout << empty << std::endl;
// } catch (const parse_error& exc) { // exception will not be thrown
// std::cout << exc.what() << std::endl;
// }
auto ip = ipv4_address::parse("192.257.2.0", err);
if (err != error_code::no_error) { // this will work as expected
std::cout << "invalid ip" << std::endl;
return 1;
}
std::cout << "ip " << ip << std::endl;
return 0;
}
Note
Keep in mind that if the IPADDRESS_NO_EXCEPTIONS flag is defined, then not all compilers can produce compile-time errors for constexpr before C++20 for parsing expressions like constexpr auto ip = ipv4_address::parse("127.0.0.257");