#Opinion on Default Exception Handling

1 messages ยท Page 1 of 1 (latest)

charred raptorBOT
#

@sacred reef has a question:

xanax_mister

What do you think about throwing exception into default in switch statement ?

#

<@&987246399047479336> please have a look, thanks.

tribal jewel
#

not fully sure what u mean. throw an exception when u think its time to throw an exception. doesnt matter if this is in a switch or not

#

๐Ÿ™‚

#

maybe u can clarify or show an example

sacred reef
#

An example

#

Like this do you prefer to add one more "case" and don't add the default at all

tribal jewel
#

u need the default

#

the switch should be exhaustive

#

either add a case for all options or add a default case

#

๐Ÿ™‚

#

there is nothing wrong with throwing in default, thats very common

#

๐Ÿ‘

sacred reef
#

โค๏ธ

#

Thanks mighty PEPE...

candid dirge
#

The general rule is that exceptions are for programmer errors

#

So if the default can't happen unless you extend an enum, or don't properly validate the input, throw an UnsupportedOperationException

#

That's my preferred one, as in "I get it, but I don't support it"

tribal jewel
#

(or also AssertionError if you consider this to be impossible based on how the code is written)

candid dirge
#

Random sidenote: wonder if not having -ea enabled suppresses AssertionError or it just disables assert statements

tribal jewel
#

just the latter

sacred reef
#

โค๏ธ