Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why are error codes being deprecated? #302

Closed
ruipacheco opened this issue May 1, 2018 · 12 comments
Closed

Why are error codes being deprecated? #302

ruipacheco opened this issue May 1, 2018 · 12 comments

Comments

@ruipacheco
Copy link

I've noticed this in the file io_context.ipp:

io_context::count_type io_context::run()
{
  asio::error_code ec;
  count_type s = impl_.run(ec);
  asio::detail::throw_error(ec);
  return s;
}

#if !defined(ASIO_NO_DEPRECATED)
io_context::count_type io_context::run(asio::error_code& ec)
{
  return impl_.run(ec);
}
#endif // !defined(ASIO_NO_DEPRECATED)

Why is the use of std::error_code being deprecated?

@ruipacheco
Copy link
Author

ruipacheco commented May 1, 2018

I wonder if this is linked to issue 143?

@vinniefalco
Copy link

It isn't all error codes being deprecated but rather, some functions are now exclusively indicating errors using exceptions. This makes sense. Remember that functions which have error_code& in their signature can still throw exceptions, the most common one being std::bad_alloc. What I think this change signifies is that io_context::run will not return errors which do not indicate exceptional circumstances.

You also see this with basic_io_object::cancel. In my opinion, this is a good change.

@ruipacheco
Copy link
Author

ruipacheco commented May 3, 2018 via email

@vinniefalco
Copy link

What happens to code that compiles with -fno-exceptions?

That's up to you. You should define ASIO_NO_EXCEPTIONS=1 and then provide your own implementation of throw_exception that does whatever you want:

https://github.com/chriskohlhoff/asio/blob/master/asio/include/asio/detail/throw_exception.hpp#L33

@ruipacheco
Copy link
Author

ruipacheco commented May 3, 2018 via email

@vinniefalco
Copy link

This seems to contradict the comment below:

I think you're reading it wrong. "Only define the throw_exception function" applies to the asio implementation not yours. In other words, if exceptions are enabled (ASIO_NO_EXCEPTIONS is not defined) then Asio will define the function throw_exception. Otherwise, you will have to define it.

If you use -fno-exceptions then you also need to define ASIO_NO_EXCEPTIONS=1.

@ruipacheco
Copy link
Author

ruipacheco commented May 3, 2018 via email

@vinniefalco
Copy link

@chriskohlhoff

If we change

"Only define the throw_exception function..." 

to

"Asio will only define the throw_exception function..."

it could be more clear.

@ruipacheco
Copy link
Author

Can you provide any examples of a custom definition of throw_exception when ASIO_NO_EXCEPTIONS is defined?

@vinniefalco
Copy link

vinniefalco commented May 4, 2018

template <typename Exception>
void throw_exception(const Exception& e)
{
    std::cerr << "Your app just shit the bed, because: " << e.what() << std::endl;
    std::terminate();
}

Alternatively you could log to a file, launch another process, etc... many choices. Just not throw.

@vinniefalco
Copy link

Has this issue been resolved?

@ruipacheco
Copy link
Author

Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants