How to throw std::exceptions with variable messages?

The standard exceptions can be constructed from a std::string: #include <stdexcept> char const * configfile = “hardcode.cfg”; std::string const anotherfile = get_file(); throw std::runtime_error(std::string(“Failed: “) + configfile); throw std::runtime_error(“Error: ” + anotherfile); Note that the base class std::exception can not be constructed thus; you have to use one of the concrete, derived classes.

Spring Resttemplate exception handling

You want to create a class that implements ResponseErrorHandler and then use an instance of it to set the error handling of your rest template: public class MyErrorHandler implements ResponseErrorHandler { @Override public void handleError(ClientHttpResponse response) throws IOException { // your error handling here } @Override public boolean hasError(ClientHttpResponse response) throws IOException { … } … Read more

How to get exception message in Python properly

If you look at the documentation for the built-in errors, you’ll see that most Exception classes assign their first argument as a message attribute. Not all of them do though. Notably,EnvironmentError (with subclasses IOError and OSError) has a first argument of errno, second of strerror. There is no message… strerror is roughly analogous to what … Read more

Does ‘finally’ always execute in Python?

“Guaranteed” is a much stronger word than any implementation of finally deserves. What is guaranteed is that if execution flows out of the whole try–finally construct, it will pass through the finally to do so. What is not guaranteed is that execution will flow out of the try–finally. A finally in a generator or async … Read more

Does a finally block run even if you throw a new Exception?

Yes, the finally blocks always runs… except when: The thread running the try-catch-finally block is killed or interrupted You use System.exit(0); The underlying VM is destroyed in some other way The underlying hardware is unusable in some way Additionally, if a method in your finally block throws an uncaught exception, then nothing after that will … Read more

In Python, how does one catch warnings as if they were exceptions?

To handle warnings as errors simply use this: import warnings warnings.filterwarnings(“error”) After this you will be able to catch warnings same as errors, e.g. this will work: try: some_heavy_calculations() except RuntimeWarning: breakpoint() P.S. Added this answer because the best answer in comments contains misspelling: filterwarnigns instead of filterwarnings.

Re-raise exception with a different type and message, preserving existing information

Python 3 introduced exception chaining (as described in PEP 3134). This allows, when raising an exception, to cite an existing exception as the “cause”: try: frobnicate() except KeyError as exc: raise ValueError(“Bad grape”) from exc The caught exception (exc, a KeyError) thereby becomes part of (is the “cause of”) the new exception, a ValueError. The … Read more

Throw HttpResponseException or return Request.CreateErrorResponse?

The approach I have taken is to just throw exceptions from the api controller actions and have an exception filter registered that processes the exception and sets an appropriate response on the action execution context. The filter exposes a fluent interface that provides a means of registering handlers for specific types of exceptions prior to … Read more

How can I rethrow an exception in Javascript, but preserve the stack?

This is a bug in Chrome. Rethrowing an exception should preserve the call trace. http://code.google.com/p/chromium/issues/detail?id=60240 I don’t know of any workaround. I don’t see the problem with finally. I do see exceptions silently not showing up on the error console in some cases after a finally, but that one seems to be fixed in development … Read more

Spring Boot REST service exception handling

New answer (2016-04-20) Using Spring Boot 1.3.1.RELEASE New Step 1 – It is easy and less intrusive to add the following properties to the application.properties: spring.mvc.throw-exception-if-no-handler-found=true spring.resources.add-mappings=false Much easier than modifying the existing DispatcherServlet instance (as below)! – JO’ If working with a full RESTful Application, it is very important to disable the automatic mapping … Read more