I'd like to discuss what's the best convention to retain when designing
exceptions to raise from a package: is it recommended to create a lot
of very specific exception types, or a single type with a field
containing the error code?

For example, let's say we want to raise an exception when trying to
convert a string between two encodings [1]. My natural choice would be
to create a StringEncodingError abstract type, with e.g. the following
concrete subtypes:
- InvalidEncodingError: specified encodings are not supported
- InvalidSequenceError: encountered invalid byte sequence
- IncompleteSequenceError: input ended with incomplete byte sequence

Is such a level of detail a good idea? Or would it be better to make
StringEncodingError a concrete type, with a field holding the error
code?

The arguments in favor of having many subtypes is that it makes it easy
to catch only a certain type of error, without catching unexpected
ones. Also, the fields of each object can be adapted to the error.

The arguments against this solution are that it would create too much
complexity for little gain.

What's the recommended way in that regard? It could be useful to say a
few words about this in the manual to help ensuring consistency across
Julia packages.


Thanks!


1: https://github.com/nalimilan/iconv.jl/pull/7/

Reply via email to