Emitting a signal inside a catch block is permitted. Consider carefully though the parameters you use for the signal, keeping in mind that client code gets to choose whether to connect to that signal directly or via a queued connection (i.e. you don't know which will be used at the time you emit the signal). In your example, your signal has a const char*
parameter and in the catch block, you use the exception's what()
function as that argument. If client code is connected to the signal via a queued connection, the const char*
argument may no longer exist at the point where the slot gets called. If you want to emit signals from a catch block, consider using only parameter types which are safe to copy (e.g. std::string
passed by value instead of const char*
).
Queued connections will be required if you want to catch an exception in a non-GUI thread and emit the signal there, then have that signal caught in the GUI thread to display some kind of error message to the user (just one example usage scenario). Queued connections are okay as long as you are careful about the parameter types you use as mentioned above. Personally, I'd consider it wise to assume the signal emitted from your catch block could be connected via a queued connection (more robust to future changes, etc).
UPDATED:
Specifically responding to the question of whether it is better or worse to use a queued connection rather than direct, it depends on what matters to you. A direct connection is the most efficient and guarantees that your code responds to the exception immediately, but you are responsible for thread safety. A direct connection is really just going to turn a signal emission into an ordinary function call, albeit with a little bit of housekeeping along the way. A queued connection will delay the response to the exception to an arbitrary point in the future even if it is responding in the same thread (a queued response will be handled when control returns to the current thread's event loop if the object owning the slot belongs to the same thread, but there could be other signals processed first).
Your question didn't mention other connection types, but the default is Qt::AutoConnection
which means the behaviour will be as for direct if the object owning the slot belongs to the same thread or queued if it belongs to a different thread, so this connection type might result in your code responding to the exception immediately or it may be delayed, which means you again need to handle thread safety yourself. A Qt::BlockingQueuedConnection
would give you a guarantee that your code responds immediately but has potential negative effects on performance and requires even more care to avoid deadlock, etc.
So while your updated question indicates that you are not asking specifically about threads, the threading considerations are strongly associated with your question of which connection type is better. Ultimately, it depends on what criteria you have for better.