Error golfing!

Hi Julians!

This is a fun and stress-free challenge for anyone who wishes to become more familiar with … ERRORS.

In https://docs.julialang.org/en/v1/manual/control-flow/#Built-in-Exceptions-1 there are 25 exceptions. Your job is to write code that throws all of them. And since this is a golfing challenge, you should try to do so with the least amount of Julia code (counted in bytes). To give everyone a fair and equal testing ground, I have set up a small repository with 25 functions that are tested. There are no rules as to what you are allowed to do, except that all the code that you use must be contained inside the src/ folder. The repository can be cloned from:

Everyone is welcome to participate!

Post your scores (and solutions) below.

5 Likes

I got a score of 587 with a bit of metaprogramming trickery. Feels a bit like cheating, but seems to be explicitely allowed. Thank you for creating this cool challenge!

module ErrorGolfing
using Distributed
for e=split(replace(replace("Argument1 Bounds1 Composite2 DimensionMismatch Divide1 Domain1 EOF1 12 Inexact1 Init1 Interrupt2 InvalidState2 Key1 Load1 OutOfMemory1 ReadOnlyMemory1 Method1 Overflow1 System1 Type1 UndefRef1 UndefVar1 StringIndex1","1"=>"Error"),"2"=>"Exception"))
@eval $(Symbol(:throw_,replace(lowercasefirst(e),r"[A-Z]+"=>s->(s=lowercase(s);s[1:end-1]*'_'*s[end]))))()=throw($(Expr(:new,Symbol(e))))end
throw_meta_parse_error()=Meta.parse(",")
throw_distributed_remote_exception()=throw(RemoteException(CapturedException(0,[])))end
2 Likes

I think it is more fun to see what kinds of solutions people come up with, rather than try to limit what people are allowed to do, so I explicitly didn’t want to prevent you from using metaprogramming. :smile:

However, I do think I would consider a solution that actually calls functions which throws all of the errors in obscure ways more beautiful, even if it has a higher score.

1 Like

Alas, seeing ERROR is already kicking off my fight-or-flight response :joy:

4 Likes

Proud of myself for not having seen some of those errors before…

1 Like