throw_errno.h.xml 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <chapter xml:id="throw_errno.h">
  2. <title><tt>__vic/throw_errno.h</tt></title>
  3. <chapter xml:id="throw_errno">
  4. <title><tt>throw_errno()</tt></title>
  5. <code-block lang="C++"><![CDATA[
  6. [[noreturn]] void throw_errno(const char *prompt);
  7. [[noreturn]] void throw_errno(const char *prompt, int err_no);
  8. ]]></code-block>
  9. <p>Throw an exception with global <tt>errno</tt> value or given
  10. <tt>err_no</tt> correspondingly. Default exception type is
  11. <xref to="libc_error"/> at the moment but can be changed at link time
  12. by overriding this functions. For example <tt>std::system_error</tt> can be
  13. used. Just create cpp-file with the following content in your project:</p>
  14. <code-block lang="C++"><![CDATA[
  15. #include<__vic/throw_errno.h>
  16. #include<system_error>
  17. //----------------------------------------------------------------------------
  18. // Override library functions to throw std::system_error
  19. //----------------------------------------------------------------------------
  20. void __vic::throw_errno(const char *prompt, int err_no)
  21. {
  22. throw std::system_error(err_no, std::system_category(), prompt);
  23. }
  24. //----------------------------------------------------------------------------
  25. ]]></code-block>
  26. <p>It's enough to override only one functions because the second one just calls
  27. <tt>throw_errno(prompt, errno)</tt>.</p>
  28. <section><title>Example</title>
  29. <code-block lang="C++"><![CDATA[
  30. ssize_t written = ::write(fd, buf, buf_size);
  31. if(written < 0) __vic::throw_errno("write");
  32. // ...
  33. ]]></code-block>
  34. </section>
  35. </chapter>
  36. </chapter>