- The c plus plus standard library
provides a few simple resources for handling
system level error conditions.
This is errno.cpp from chapter eight of the exercise files,
the cerrno header file provides access
to the integer value eerno
which we see down here in this print f statement.
If the errno or error number is not zero,
it will contain a value representing an error.
The supported values and their meanings
are typically defined by the host operating system.
You'll also notice this p error function.
Each error number is associated with
a corresponding string explanation.
The p error function is easiest way
to look up the error string,
takes a string argument, sends it to standard air
along with the error message
corresponding to the current value of errno.
So when I build and run this,
as we try to erase a file that doesn't exist
and we get back this error number two, and the message,
no such file or directory.
Now, if you don't want to use the p error function, instead,
you can use a star error function that returns the string,
and for this, we need the c string library
rather the c string header where it's defined.
I can come down here and I can get the error string
with these star error function
and pass it our error number.
And then I can use printout for whatever I want to,
to print that error message.
Let's put it in quotes there,
put out the error number and the string.
And when I build and run,
you notice we get error two no such file or directory.
So we can use this to print the err how we want to,
we can put it in a database
or a log or however use it, however we like.
Error numbers are very common in the standard library
and these functions make it convenient to
report errors based on the error numbers.