How to use the built-in InvalidStateException

  1. What would be a typical use case for employing the built-in InvalidStateException?

  2. Can this exception be employed for inconsistent values of different variables during runtime, i.e. invalid states or inconsistencies of different variable? Or is there a better option?

  3. Throwing InvalidStateException requires two arguments a message and a symbol. What would be typical values of a symbol?

It is up to you when to throw it, one should match the words in the exception to the kind of error. The arguments are again, situation dependent and are most likely literals.

Here’s an example from the wild

https://github.com/julia-vscode/LanguageServer.jl/issues/1009

Which if we trace back to where it is thrown

and here’s the documentation for that call

https://docs.julialang.org/en/v1/base/parallel/#Base.take!-Tuple{Channel}

What seems to be different in Julia is the docs don’t mention that take! might throw and we have no way of annotating that. Meaning one day you might log in to find you didn’t use try in your long running code and you can make a sad face.

1 Like