Should I use exceptions in a JUCE library?


#1

Background: I’m writing an AudioFormat based on the mpg123 library. There are possibilities for errors and I’m contemplating how to deal with them.

These errors involve the mp3 library being misconfigured or being unable to get the (very small) amount of memory it needs for its own purposes.

I’m thinking that this is a good use for exceptions - because this isn’t something I can really recover from gracefully at all, and because this most likely means that everything else is screwed up as well. I’m a little trepidatious because I’ve never dealt with exceptions in C++, but my code should all be exception safe, and anyway, by the time I throw these exceptions, we’re in a sorry state.

Anything I should know about JUCE and exceptions? In particular, I want to be sure that an exception will cause a JUCE application to fail… :smiley:


#2

Exceptions can be extremely tricky in c++, and it seems that the more you learn about them, the more difficult it gets… In my refactoring of the codebase this year I’ve been paying attention to exception-safety, but I’m sure there are still places where an unexpected exception could confuse something. Probably not in a dangerous way though, and there shouldn’t be any resource leaks.

Of course if you’re throwing and catching entirely within your own code, then you’ll be fine. The best advice would be that if you’re going to throw something, be careful not to have code-paths where it could propagate up the call stack into code that may not be expecting it. (But I guess that’s true in general, not only for juce).


#3

I decided not to do it. Integer error codes appear in the system’s code already - I’m sticking with that.