3
\$\begingroup\$

I was pondering today about how many characters it would take in each language to throw an exception, and noticed there wasn't any existing collection of answers, so:

The task

Throw an exception in the least amount of characters. If your language does not have exceptions, then a panic is valid. A panic constitutes as anything that there is no reasonable recovery strategy for. Examples include (but are not limited to):

  • Explicit panic instructions
  • Memory accesses that will always fail
  • Integer division by zero
  • Taking the head of an empty list in a data-based language

Things that do not count as panics:

  • Compile-/parse-time errors (such as syntax errors and invalid types in a statically-typed language)
  • Unbound/undefined identifier errors
  • Quoting/splicing errors that are not the result of runtime data (ex #'a in Lisp is truly an unbound identifier error)

Examples:

Scala (.sc): 3 Characters

???

Scala 3 (.scala): 16 Characters

@main def a= ???

Python: 3 Characters

1/0

Zig: 28 Characters

pub fn main()u8{@panic("");}
\$\endgroup\$
6
  • 7
    \$\begingroup\$ I don't think the separation you are making between types of errors is clear enough for this to work out. "No reasonable recovery strategy" would make me think that something that could be captured by a "try" block or similar structure shouldn't count, but of course most languages that define exceptions do it specifically to allow this behaviour. I find the large number of disagreements validating, so I'm going to vote to close this for now. \$\endgroup\$ Nov 23 '21 at 1:02
  • \$\begingroup\$ Anything that can be captured as a "try" block counts as an exception, and thus the language has exceptions. \$\endgroup\$
    – Aly
    Nov 23 '21 at 1:03
  • 1
    \$\begingroup\$ @Aly Do you post to sandbox? \$\endgroup\$
    – Fmbalbuena
    Nov 23 '21 at 1:08
  • 2
    \$\begingroup\$ I agree with FryAmTheEggman here. I think it is clear and unambiguous to differentiate between compiler errors and runtime errors (for languages that even have a compiler), and it's clear and unambiguous to distinguish between output to STDERR and STDOUT. But a lot of the other distinctions are super fuzzy. It seems that you are making some pretty big assumptions about language features, and on top of that your lists aren't exhaustive so there has to be a lot of guess work for what is left. \$\endgroup\$
    – Wheat Wizard
    Nov 23 '21 at 1:35
  • 5
    \$\begingroup\$ To be clear I've closed this to prevent any further answers from piling up, there seems already to be some answers which are questionable, and the longer this stays open before being clarified the more cleanup that makes for moderators when it does get clarified. Once things are nice and clear I'd be glad to open it again. \$\endgroup\$
    – Wheat Wizard
    Nov 23 '21 at 1:50
5
\$\begingroup\$

Motorola 6800 machine code, 2 1 byte

FD   HCF      ; halt and catch fire

From Some Guy:

It doesn’t really destroy the CPU, but makes it switch into a kind of debugging mode. Allegedly, HCF instruction on the 6800 should toggle the address lines in order very quickly. Legend added: up to the point that some support component might catch fire!

Fire would count as a panic, right?

\$\endgroup\$
3
\$\begingroup\$

Jelly, 1 byte

s

Try it online!

I just tried random 1-letter commands until it worked.

\$\endgroup\$
3
\$\begingroup\$

Vyxal, 1 byte

Try it Online!

Because I forgot to make integer division account for division by 0.

Better Explanation

Okay so normal division (/) accounts for division by 0 - it just always returns 0 (which has helpful uses for code golf). This is done in the overloads internally:

​def​ ​divide​(​lhs​, ​rhs​): 
 ​    ​types​ ​=​ ​vy_type​(​lhs​), ​vy_type​(​rhs​) 
  
 ​    ​def​ ​handle_numbers​(​lhs​, ​rhs​): 
 ​        ​if​ ​rhs​ ​==​ ​0​: 
 ​            ​return​ ​0 
 ​        ​normal​, ​int_div​ ​=​ ​lhs​ ​/​ ​rhs​, ​lhs​ ​//​ ​rhs 
 ​        ​return​ [​normal​, ​int_div​][​normal​ ​==​ ​int_div​] 
  
 ​    ​return​ { 
 ​        (​Number​, ​Number​): ​lambda​: ​handle_numbers​(​lhs​, ​rhs​),

That's taken directly from the vyxal source code btw.

Now the problem is that I never actually special cased division by 0 in the overloads for integer division:

 
 ​def​ ​integer_divide​(​lhs​, ​rhs​): 
 ​    ​types​ ​=​ ​vy_type​(​lhs​), ​vy_type​(​rhs​) 
 ​    ​return​ { 
 ​        (​Number​, ​Number​): ​lambda​: ​lhs​ ​//​ ​rhs​,

As you can see, it just straight up tries to do python integer division without accounting for the special case normal division handles.

Also, just for clarification, if there aren't any inputs provided to a vyxal program, 0 is implicitly used.

Alternate solutions

°

Try it Online!

How you gonna push the function reference of an undefined function?

Try it Online!

Same thing with variables

\$\endgroup\$
3
\$\begingroup\$

C (clang), 9 bytes

f(){f();}

Try it online!

No exceptions in C, I hope a scary segfault with exit code is fine.

\$\endgroup\$
3
\$\begingroup\$

This isn't the shortest by any means, but here it is:

C++ (g++), 14 bytes

main(){throw;}
\$\endgroup\$
5
  • \$\begingroup\$ Are you sure that 1/0 is an exception or panic? \$\endgroup\$
    – BWG
    Nov 23 '21 at 1:32
  • \$\begingroup\$ On my system w/ gcc the compiler is too smart to fall for 1/0 :) So the shortest I could get was main(a){a/=!a;} and I knew someone would come up with something shorter for C. \$\endgroup\$
    – cnamejj
    Nov 23 '21 at 1:35
  • \$\begingroup\$ @BWG I would assume, but as I said, I'm not getting any output from it on my Linux machine, so maybe it is not... I'll mark it as a maybe-works answer. \$\endgroup\$
    – LorenDB
    Nov 23 '21 at 12:37
  • \$\begingroup\$ Hmm, looks like to make it be an exception you have to assign it: f=1/0; \$\endgroup\$
    – LorenDB
    Nov 24 '21 at 0:05
  • \$\begingroup\$ Never mind. I should have remembered that C++ won't let you drop the type specifier like C apparently does at places. Removing. \$\endgroup\$
    – LorenDB
    Nov 24 '21 at 0:11
2
\$\begingroup\$

JavaScript (V8), 3 bytes

0``

Try it online!

JavaScript (V8), 3 bytes

0()

Try it online!

Not sure if you can get it below 3.

\$\endgroup\$
5
  • \$\begingroup\$ tio.run/##y0osSyxOLsosKNEts/j/X/P/fwA \$\endgroup\$
    – Leaky Nun
    Nov 23 '21 at 0:50
  • \$\begingroup\$ @LeakyNun Judging by the examples, I don't think SyntaxErrors are valid. \$\endgroup\$ Nov 23 '21 at 0:54
  • \$\begingroup\$ "If your language does not have exceptions, then a panic is valid" \$\endgroup\$
    – Leaky Nun
    Nov 23 '21 at 0:55
  • 1
    \$\begingroup\$ A parse-time error is not a panic, as it happens before any code is run. \$\endgroup\$
    – Aly
    Nov 23 '21 at 0:57
  • 1
    \$\begingroup\$ Yeah but it is an exception. You only defined "panic" for languages without excceptions. \$\endgroup\$
    – Leaky Nun
    Nov 23 '21 at 0:58
2
\$\begingroup\$

Bash, 4 bytes

. $0

Try it online!

Calls itself (well includes itself more accurately) until it segfaults.

Aubergine, 3 bytes

===

Try it online!

As far as I know, all Aubergine programs have to be divisible by 3, since all instructions are 3 characters. There are lots of 3 character programs that will fail. Here's one that I think is aesthetically pleasing. :)

Ly, 1 byte

I

Try it online!

Tries to pop from an empty stack. There are other one character programs in Ly that will fail when there's nothing on the stack.

\$\endgroup\$
5
  • \$\begingroup\$ Any reason that just . (without the ` $0`) shouldn't be considered an exception? I'm not familiar with how Bash handles errors. \$\endgroup\$
    – Aly
    Nov 23 '21 at 1:01
  • \$\begingroup\$ Running . does produce an error on TryItOnline, but I'm not sure it's panicky/exceptiony enough to qualify for this question? \$\endgroup\$
    – cnamejj
    Nov 23 '21 at 1:07
  • \$\begingroup\$ Ah, you'd be right: It doesn't halt execution of the rest of the program. \$\endgroup\$
    – Aly
    Nov 23 '21 at 1:11
  • \$\begingroup\$ By the way, this challenge is closed, but if you have multiple solutions, you should post multiple answers. \$\endgroup\$
    – user
    Nov 23 '21 at 1:50
  • \$\begingroup\$ Thanks! I've gotten conflicting guidance on posting multiple programs in one posting or as separate postings. So I'm not sure how to tell which way to go for a given question. \$\endgroup\$
    – cnamejj
    Nov 23 '21 at 2:36
2
\$\begingroup\$

!@#$%^&*()_+, 3 bytes

^_@

Try it online!

Converts -1 to ascii but errors.

\$\endgroup\$
0
\$\begingroup\$

Malbolge Unshackled, 0 bytes

Try it online!

I have no idea, don't ask me why it works.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Things that do not count as panics: Compile time errors - I'd say error: not a valid Malbolge program counts as a sort of compile time error \$\endgroup\$
    – lyxal
    Nov 23 '21 at 0:57
  • \$\begingroup\$ Why is this not a valid Malbolge program? As someone who has never written Malbolge, it seems like it should accept an empty tape just fine. \$\endgroup\$
    – Aly
    Nov 23 '21 at 0:58
  • \$\begingroup\$ @Aly The initial state of the memory depends on the last two characters of the program, so is undefined for shorter programs. Agreed, for what it's worth, that this is a compile-time error and thus invalid. \$\endgroup\$ Nov 24 '21 at 3:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.