Halt and catch fire: Difference between revisions
Puppydrum64 (talk | contribs) No edit summary |
(Added Easylang) |
||
(112 intermediate revisions by 52 users not shown) | |||
Line 1: | Line 1: | ||
{{task}} |
|||
[[Category:Simple]] |
|||
;Task |
;Task |
||
Create a program that crashes as soon as possible, with as few lines of code as possible. Be smart and don't damage your computer, ok? |
Create a program that crashes as soon as possible, with as few lines of code as possible. Be smart and don't damage your computer, ok? |
||
The code should be syntactically valid. It should be possible to insert [a subset of] your submission into another program, presumably to help debug it, or perhaps for use when an internal corruption has been detected and it would be dangerous and irresponsible to continue. |
|||
;References |
|||
* [[wp:Halt_and_Catch_Fire_(computing)|Wikipedia: Halt and Catch Fire]] |
|||
;Related Tasks |
|||
* [[Program termination]] |
|||
<br><br> |
|||
=={{header|11l}}== |
|||
{{trans|Nim}} |
|||
<syntaxhighlight lang="11l">assert(0B)</syntaxhighlight> |
|||
=={{header|6502 Assembly}}== |
=={{header|6502 Assembly}}== |
||
Upon executing this byte as code, the processor will halt. No interrupts can occur either. This does not occur on 65c02-based hardware such as the Apple II or Atari Lynx. |
Upon executing this byte as code, the processor will halt. No interrupts can occur either. This does not occur on 65c02-based hardware such as the Apple II or Atari Lynx. |
||
<lang |
<syntaxhighlight lang="6502asm"> db $02</syntaxhighlight> |
||
This version works on all 6502 models: |
|||
<syntaxhighlight lang="6502asm">forever: |
|||
jmp forever</syntaxhighlight> |
|||
This code is often written as <code>JMP $</code> which means the same thing. (In addition to the hexadecimal token, $ can refer to the value of the program counter at that instruction's address. |
|||
=={{header|8080 Assembly}}== |
|||
{{trans|Z80 Assembly}} |
|||
<syntaxhighlight lang="8080asm">di |
|||
hlt</syntaxhighlight> |
|||
=={{header|8086 Assembly}}== |
|||
{{trans|Z80 Assembly}} |
|||
Disabling interrupts prior to a <code>HLT</code> command will cause the CPU to wait forever. |
|||
<syntaxhighlight lang="asm">cli |
|||
hlt</syntaxhighlight> |
|||
=={{header|68000 Assembly}}== |
|||
If interrupts are disabled, a jump instruction that jumps to itself will do just fine. |
|||
<syntaxhighlight lang="68000devpac">jmp * ;many assemblers allow * or $ to represent the address of this line of code.</syntaxhighlight> |
|||
=={{header|Ada}}== |
|||
<syntaxhighlight lang="ada">procedure Halt_And_Catch_Fire is |
|||
begin |
|||
raise Program_Error with "Halt and catch fire"; |
|||
end Halt_And_Catch_Fire;</syntaxhighlight> |
|||
{{out}} |
|||
<pre>raised PROGRAM_ERROR : Halt and catch fire |
|||
</pre> |
|||
=={{header|ALGOL 68}}== |
|||
This program will crash immediately on startup. |
|||
<syntaxhighlight lang="algol68">( print( ( 1 OVER 0 ) ) )</syntaxhighlight> |
|||
=={{header|ALGOL W}}== |
|||
This won't halt the CPU but the program will crash immediately on startup. |
|||
<syntaxhighlight lang="algolw">assert false.</syntaxhighlight> |
|||
=={{header|Applesoft BASIC}}== |
|||
The $02 op code won't crash the Apple IIGS. Here is the [[6502_Assembly|6502 Assembly]] for a relocatable infinite loop consisting of 3 bytes: |
|||
<pre> |
|||
:B8 CLV |
|||
:50 FE BVC {-02}</pre> |
|||
This is a one-liner that embeds the 3 bytes in a string, and calls the code contained within the string. |
|||
<syntaxhighlight lang="gwbasic">HCF$ = CHR$ (184) + "P" + CHR$ (254): CALL PEEK ( PEEK (131) + PEEK (132) * 256 + 1) + PEEK ( PEEK (131) + PEEK (132) * 256 + 2) * 256</syntaxhighlight> |
|||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="rebol">0/0</syntaxhighlight> |
|||
{{out}} |
|||
<pre>>> Runtime | File: halt and catch fire.art |
|||
error | Line: 1 |
|||
| |
|||
| uncaught system exception: |
|||
| division by zero</pre> |
|||
=={{header|AWK}}== |
|||
<syntaxhighlight lang="awk"> |
|||
# syntax: GAWK -f HALT_AND_CATCH_FIRE.AWK |
|||
# |
|||
# This won't halt the CPU but the program will crash immediately on startup |
|||
# with "error: division by zero attempted". |
|||
BEGIN { 1/0 } |
|||
# |
|||
# This will heat up the CPU, don't think it will catch on fire. |
|||
BEGIN { while(1) {} } |
|||
# |
|||
# Under TAWK 5.0 using AWKW will immediately abort. |
|||
BEGIN { abort(1) } |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
gawk: C:\AWK\HALT_AND_CATCH_FIRE.AWK:5: error: division by zero attempted |
|||
</pre> |
|||
=={{header|Binary Lambda Calculus}}== |
|||
BLC forces normal programs to start with a closed lambda term, by mapping free variables to the divergent Omega = <code>(\x.x x)(\x.x x)</code>, the lambda calculus equivalent of an infinite loop. That makes the following 2-bit BLC program the smallest to catch fire: |
|||
<pre>10</pre> |
|||
=={{header|BQN}}== |
|||
The easiest way to terminate with an error is using assert (<code>!</code>): |
|||
<syntaxhighlight lang="bqn">! "Insert value that is not 1" |
|||
"Error Message" ! "Value that is not 1, again"</syntaxhighlight> |
|||
Other runtime errors are possible, but not as easy to use. |
|||
=={{header|Bruijn}}== |
|||
Bruijn does not have runtime errors. For debugging you can either write tests (which are run before evaluating main) or use tactical infinite loops: |
|||
<syntaxhighlight lang="bruijn"> |
|||
:test ([[0]]) ([[1]]) |
|||
main [[0 0] [0 0]] |
|||
</syntaxhighlight> |
|||
=={{header|C}}== |
|||
<syntaxhighlight lang="c">int main(){int a=0, b=0, c=a/b;}</syntaxhighlight> |
|||
=={{header|C++}}== |
|||
Use an unhandled exception to crash the program. |
|||
<syntaxhighlight lang="cpp">#include <stdexcept> |
|||
int main() |
|||
{ |
|||
throw std::runtime_error("boom"); |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
terminate called after throwing an instance of 'std::runtime_error' |
|||
what(): boom |
|||
</pre> |
|||
The output depends on the compiler and platform but should be similar. |
|||
=={{header|C sharp}}== |
|||
{{works with|C sharp|9}} |
|||
This throws a DivideByZeroException at runtime.<br/> |
|||
<syntaxhighlight lang="csharp">int a=0,b=1/a;</syntaxhighlight> |
|||
This will throw a compile-time exception, so technically not a valid solution. |
|||
<syntaxhighlight lang="csharp">int a=1/0;</syntaxhighlight> |
|||
This one-liner also works |
|||
<syntaxhighlight lang="csharp">throw new System.Exception();</syntaxhighlight> |
|||
=={{header|Computer/zero Assembly}}== |
|||
<syntaxhighlight lang="6502asm">STP</syntaxhighlight> |
|||
=={{header|Crystal}}== |
|||
<syntaxhighlight lang="crystal">raise "fire"</syntaxhighlight> |
|||
=={{header|Delphi}}== |
|||
{{works with|Delphi|6.0}} |
|||
{{libheader|SysUtils,SysUtils,StdCtrls}} |
|||
The program uses Delphi's builtin exception processing to throw an exception. Uncaught exceptions abort a program |
|||
<syntaxhighlight lang="Delphi"> |
|||
procedure HaltAndCatchFire; |
|||
begin |
|||
raise Exception.Create('Burning to the ground'); |
|||
end; |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
[[File:DelphiHaltCatchFire.png|thumb|none]] |
|||
<pre> |
|||
</pre> |
|||
=={{header|EasyLang}}== |
|||
<syntaxhighlight> |
|||
a[] = [ ] |
|||
print a[1] |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
*** ERROR: index out of bounds |
|||
</pre> |
|||
=={{header|F_Sharp|F#}}== |
|||
<syntaxhighlight lang="fsharp"> |
|||
0/0 |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[ERROR] FATAL UNHANDLED EXCEPTION: System.DivideByZeroException: Attempted to divide by zero. |
|||
exit status 1 |
|||
</pre> |
|||
=={{header|Factor}}== |
|||
===REPL=== |
|||
Causing a stack underflow is trivial; just call any word that expects arguments with an empty data stack. |
|||
<syntaxhighlight lang="factor">+</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Data stack underflow |
|||
</pre> |
|||
===script=== |
|||
This crashes because Factor expects the data stack to be empty at the end of a program. However, it is not here. |
|||
<syntaxhighlight lang="factor">1</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Quotation's stack effect does not match call site |
|||
quot [ 1 ] |
|||
call-site ( -- ) |
|||
(U) Quotation: [ c-to-factor => ] |
|||
Word: c-to-factor |
|||
(U) Quotation: [ [ (get-catchstack) push ] dip call => (get-catchstack) pop* ] |
|||
(O) Word: command-line-startup |
|||
(O) Word: run |
|||
(O) Word: load-vocab |
|||
(O) Method: M\ vocab (require) |
|||
(O) Word: load-source |
|||
(O) Word: wrong-values |
|||
(O) Method: M\ object throw |
|||
(U) Quotation: [ |
|||
OBJ-CURRENT-THREAD special-object error-thread set-global |
|||
current-continuation => error-continuation set-global |
|||
[ original-error set-global ] [ rethrow ] bi |
|||
</pre> |
|||
===deployed=== |
|||
When deploying as a standalone executable, a main word and vocabulary must be declared. The stack effect checker must be satisfied, so we can't rely on either of the tricks used before. Therefore <code>die</code> is called instead. |
|||
<syntaxhighlight lang="factor">USE: kernel IN: a : b ( -- ) die ; MAIN: b</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
You have triggered a bug in Factor. Please report. |
|||
critical_error: The die word was called by the library.: 0 |
|||
Starting low level debugger... |
|||
Basic commands: |
|||
q ^Z -- quit Factor |
|||
c -- continue executing Factor - NOT SAFE |
|||
t -- throw exception in Factor - NOT SAFE |
|||
.s .r .c -- print data, retain, call stacks |
|||
help -- full help, including advanced commands |
|||
> |
|||
</pre> |
|||
=={{header|FALSE}}== |
|||
Any function with the exception of <code>^</code> (read from stdin) or <code>ß</code> (flush stdin) will cause a stack underflow. |
|||
<syntaxhighlight lang="false">.</syntaxhighlight> |
|||
Alternatively, the FALSE interpreter expects the stack to be empty at the end of the program's execution, and so leaving a value on the stack is also a valid strategy for crashing the program. |
|||
<syntaxhighlight lang="false"> 0</syntaxhighlight> |
|||
=={{header|Fortran}}== |
|||
{{works with|Fortran|77,90,95,...}} |
|||
<syntaxhighlight lang="fortran"> PROGRAM A |
|||
CALL ABORT |
|||
END</syntaxhighlight> |
|||
=={{header|Fermat}}== |
|||
Defines, then calls, a function with no parameters that calls itself. A segfault occurs. |
|||
<syntaxhighlight lang="fermat">Func S=S. S;</syntaxhighlight> |
|||
This alternative is five bytes longer but crashes more thoroughly; after a warning about end of line inside a string literal it locks my computer up for a good 2-3 minutes before exiting to the command prompt. |
|||
<syntaxhighlight lang="fermat">while 1 do !' od;</syntaxhighlight> |
|||
=={{header|FreeBASIC}}== |
|||
Instant segfault. |
|||
<syntaxhighlight lang="freebasic">poke 0,0</syntaxhighlight> |
|||
This alternative crashes the compiler. |
|||
<syntaxhighlight lang="freebasic">#define A() B() |
|||
#define B() A() |
|||
A()</syntaxhighlight> |
|||
=={{header|GDScript}}== |
|||
An empty script will run and immediately error due to not inheriting from Node: |
|||
<code>Script inherits from native type 'RefCounted', so it can't be assigned to an object of type: 'Node'</code> |
|||
A script with zero warnings: |
|||
<syntaxhighlight lang="gdscript"> |
|||
extends Node |
|||
func _init():$a.a() |
|||
</syntaxhighlight> |
|||
<pre> |
|||
E 0:00:00:0321 halt_and_catch_fire.gd:2 @ _init(): Node not found: "a" (relative to "Node"). |
|||
<C++ Error> Method/function failed. Returning: nullptr |
|||
<C++ Source> scene/main/node.cpp:1364 @ get_node() |
|||
<Stack Trace> halt_and_catch_fire.gd:2 @ _init() |
|||
</pre> |
|||
This attempts to call a method on a nonexistent child node (just accessing without calling will produce a warning <code>Standalone expression (the line has no effect).</code> |
|||
=={{header|Go}}== |
|||
This wouldn't survive ''go fmt'' which would stretch it out to 5 lines. However, that's not compulsory and the task says do it in as few lines as possible. |
|||
<syntaxhighlight lang="go">package main; import "fmt"; func main(){a, b := 0, 0; fmt.Println(a/b)}</syntaxhighlight> |
|||
<br> |
|||
An alternative shorter line would be: |
|||
<syntaxhighlight lang="go">package main; func main(){panic(0)}</syntaxhighlight> |
|||
=={{header|GW-BASIC}}== |
|||
<syntaxhighlight lang="gwbasic">0 gosub 0</syntaxhighlight> |
|||
=={{header|Hare}}== |
|||
<syntaxhighlight lang="hare">export fn main() void = abort();</syntaxhighlight> |
|||
=={{header|Haskell}}== |
|||
An alternative to the following is to use ''undefined''. |
|||
<syntaxhighlight lang="haskell">main = error "Instant crash"</syntaxhighlight> |
|||
=={{header|J}}== |
|||
<syntaxhighlight lang="j"> (1e6$a.) memw (mema 1),0 1e6</syntaxhighlight> |
|||
In other words: allocate one byte of memory and write 1e6 bytes starting at that address. |
|||
It's probably more effective to use <syntaxhighlight lang="j"> exit 0</syntaxhighlight> -- this approach would eliminate dependence on a variety of implementation details. |
|||
=={{header|Java}}== |
|||
<syntaxhighlight lang="java"> |
|||
public final class HaltAndCatchFire { |
|||
public static void main(String[] aArgs) { |
|||
// Any one of the lines below, when uncommented, will cause a program halt. |
|||
// throw new AssertionError("Stop now!"); |
|||
// System.out.println(0/0); |
|||
// Runtime.getRuntime().exit(1); |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|jq}}== |
|||
{{works with|jq}} |
|||
Also works with gojq, the Go implementation of jq |
|||
The polite way to halt a running jq program is to use `error` or `halt_error`, both of which come in two flavors. |
|||
For example: |
|||
<syntaxhighlight lang=jq> |
|||
"whoops" | error |
|||
</syntaxhighlight> |
|||
or |
|||
<syntaxhighlight lang=jq> |
|||
0 | error("whoops") |
|||
</syntaxhighlight> |
|||
It is worth noting that the text of a run-time error can be captured using `error/1`, e.g. |
|||
<pre> |
|||
$ jq -n '0 as $x | try (1/$x) catch error("The error text is: \(.)")' |
|||
jq: error (at <unknown>): The error text is: number (1) and number (0) cannot be divided because the divisor is zero |
|||
</pre> |
|||
"Catching fire" is not so easily done. |
|||
=={{header|Julia}}== |
|||
To crash the running program: |
|||
<syntaxhighlight lang="julia">@assert false "Halt and catch fire."</syntaxhighlight>{{out}}<pre>ERROR: AssertionError: Halt and catch fire.</pre> |
|||
To crash the LLVM virtual machine running Julia with Exception: EXCEPTION_ILLEGAL_INSTRUCTION: |
|||
<syntaxhighlight lang="julia">unsafe_load(convert(Ptr{Int}, C_NULL))</syntaxhighlight> |
|||
=={{header|Liberty BASIC}}== |
|||
This is just one possibility. |
|||
<syntaxhighlight lang="lb">Let</syntaxhighlight> |
|||
=={{header|Lua}}== |
|||
Tricks could be used to shorten this, particularly from interactive REPL, where <code>-_</code> would be enough (i.e., attempt arithmetic on a nil global), or from a file <code>_()</code> would be enough (i.e., attempt to call a nil global). This instead focuses on the "be useful elsewhere" aspect of the task, because both seem short-enough as-is: |
|||
<syntaxhighlight lang="lua">error(1)</syntaxhighlight> |
|||
{{out}} |
|||
<pre>1 |
|||
stack traceback: |
|||
[C]: in function 'error' |
|||
stdin:1: in main chunk |
|||
[C]: in ?</pre> |
|||
Or: |
|||
<syntaxhighlight lang="lua">assert(false)</syntaxhighlight> |
|||
{{out}} |
|||
<pre>stdin:1: assertion failed! |
|||
stack traceback: |
|||
[C]: in function 'assert' |
|||
stdin:1: in main chunk |
|||
[C]: in ?</pre> |
|||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
|||
<syntaxhighlight lang="mathematica">Abort[]</syntaxhighlight> |
|||
=={{header|Nim}}== |
|||
One possibility: |
|||
<syntaxhighlight lang="nim">assert false</syntaxhighlight> |
|||
Another solution with the same number of characters (we could also use <code>mod</code> instead of <code>div</code>): |
|||
<syntaxhighlight lang="text">echo 1 div 0</syntaxhighlight> |
|||
But the shortest solution may be: |
|||
<syntaxhighlight lang="nim">assert 1==0</syntaxhighlight> |
|||
=={{header|Pascal}}== |
|||
{{Works with|Free Pascal}} Do an illegal memory access at $0 |
|||
<syntaxhighlight lang="pascal">begin pByte($0)^ := 0 end.</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Runtime error 216 at $0000000000401098</pre> |
|||
=={{header|Perl}}== |
|||
This is not a syntax error, it is a fatal run time error. See "perldoc perldiag". |
|||
<syntaxhighlight lang="perl">&a</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Undefined subroutine &main::a called at line 1.</pre> |
|||
=={{header|PL/M}}== |
|||
This will terminate the program by restarting CP/M. |
|||
<syntaxhighlight lang="plm">100H: GOTO 0; EOF</syntaxhighlight> |
|||
=={{header|Phix}}== |
|||
I normally and quite often just use this: |
|||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
<span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span> |
|||
<!--</syntaxhighlight>--> |
|||
The ? means print and/but obviously the 9/0 triggers a fatal error before it gets that far. |
|||
{{out}} |
|||
<pre> |
|||
C:\Program Files (x86)\Phix\test.exw:1 |
|||
attempt to divide by 0 |
|||
Global & Local Variables |
|||
--> see C:\Program Files (x86)\Phix\ex.err |
|||
Press Enter... |
|||
</pre> |
|||
Alternatives include crash("some message") which produces similar output, and abort(n) which is somewhat quieter with abort(0) meaning (immediately) terminate normally without an error. All of those can be caught by try/catch: should you want to get properly brutal and defeat any active exception handler you can/must resort to inline assembly: |
|||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
<span style="color: #008080;">try</span> |
|||
#ilASM{ |
|||
[PE32] |
|||
push 1 -- uExitCode |
|||
call "kernel32","ExitProcess" |
|||
[PE64] |
|||
sub rsp,8*5 |
|||
mov rcx,1 -- uExitCode |
|||
call "kernel32","ExitProcess" |
|||
[ELF32] |
|||
xor ebx, ebx |
|||
mov eax, 1 -- SYSCALL_EXIT |
|||
int 0x80 |
|||
[ELF64] |
|||
mov rax,231 -- sys_exit_group(rdi=int error_code) |
|||
xor rdi,rdi |
|||
syscall |
|||
} |
|||
<span style="color: #008080;">catch</span> <span style="color: #000000;">e</span> |
|||
<span style="color: #0000FF;">?</span><span style="color: #000000;">e</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">try</span> |
|||
<!--</syntaxhighlight>--> |
|||
No output, the try/catch is just for show. ExitProcess/sys_exit are the only non-catchable things I know of, apart from a few other deliberates such as quitting the debugger, and aside from being technically difficult to catch it seems reasonable to classify them as direct actions rather than errors, and that way excuse the non-catchableness.<br> |
|||
<small>(I suppose [ok, actually know that] you could also write inline assembly that fubars the call stack to [effectively or quite deliberately] disable any active exception handler[s])</small> |
|||
=={{header|Python}}== |
|||
<syntaxhighlight lang="python">0/0</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Traceback (most recent call last): |
|||
File "<stdin>", line 1, in <module> |
|||
ZeroDivisionError: division by zero |
|||
</pre> |
|||
=={{header|Quackery}}== |
|||
Ripping lumps out of core definitions will do the trick. |
|||
<pre> > quackery |
|||
Welcome to Quackery. |
|||
Enter "leave" to leave the shell. |
|||
Building extensions. |
|||
/O> ' tuck take |
|||
... |
|||
Quackery system damage detected. |
|||
Python reported: maximum recursion depth exceeded</pre> |
|||
=={{header|Raku}}== |
|||
<syntaxhighlight lang="raku" line>++8</syntaxhighlight> |
|||
Syntactically: Valid. |
|||
Semantically: Change the mathematical concept of 8 to 9, either in your whole computer, or maybe the whole universe. |
|||
Fails with this run-time error: |
|||
{{out}} |
|||
<pre>Cannot resolve caller prefix:<++>(Int:D); the following candidates |
|||
match the type but require mutable arguments: |
|||
(Mu:D $a is rw) |
|||
(Int:D $a is rw --> Int:D) |
|||
The following do not match for other reasons: |
|||
(Bool $a is rw) |
|||
(Mu:U $a is rw) |
|||
(Num:D $a is rw) |
|||
(Num:U $a is rw) |
|||
(int $a is rw --> int) |
|||
(num $a is rw --> num) |
|||
in block <unit> at -e line 1</pre> |
|||
Alternately, and perhaps more community condoned, to end the program as soon as possible without trying to change the Laws of the Universe, you could just enter: |
|||
<syntaxhighlight lang="raku" line>die</syntaxhighlight> |
|||
{{out|In REPL}} |
|||
<pre>Died |
|||
in block <unit> at <unknown file> line 1</pre> |
|||
Same character count, exits the program as soon as possible (though trappable if desired through the exception system,) and it looks more like an intentional act rather than a typo. Plus, you can add a message that will be added ''when'' it dies to explain why. |
|||
Here is a silly alternative : A standalone Unicode counterpart for the [https://docs.raku.org/language/operators#listop_... yada yada yada] operator takes up 3 code units but visually just a single codepoint, |
|||
{{out}} |
|||
<pre>cat test.raku ; wc test.raku |
|||
… |
|||
1 1 4 test.raku |
|||
raku -c test.raku ; echo $? |
|||
Syntax OK |
|||
0 |
|||
raku test.raku ; echo $? |
|||
Stub code executed |
|||
in block <unit> at test.raku line 1 |
|||
1</pre> |
|||
However when I tried to combine all to test against the Test module, the last one somehow lived through an EVAL, |
|||
<syntaxhighlight lang="raku" line>use Test; |
|||
dies-ok { ++8 }; |
|||
dies-ok { die }; |
|||
dies-ok { … }; |
|||
eval-dies-ok '++8'; |
|||
eval-dies-ok 'die'; |
|||
eval-dies-ok '…' ;</syntaxhighlight> |
|||
{{out}} |
|||
<pre>ok 1 - |
|||
ok 2 - |
|||
ok 3 - |
|||
ok 4 - |
|||
ok 5 - |
|||
not ok 6 - |
|||
# Failed test at all.raku line 11</pre> |
|||
so it is indeed at one's discretion whether this one is qualified as a crasher. |
|||
=={{header|REXX}}== |
|||
===Version 1=== |
|||
<syntaxhighlight lang="rexx">_=1;_+=</syntaxhighlight> |
|||
<pre> |
|||
There is no output shown in the DOS window. This REXX program (using Regina REXX) also |
|||
crashes the Microsoft DOS window (application). |
|||
ooRexx shows this: |
|||
H:\>rexx c2 |
|||
1 *-* _+= |
|||
Error 35 running H:\c2.rex line 1: Invalid expression. |
|||
Error 35.918: Missing expression following assignment instruction.</pre> |
|||
===Version 2=== |
|||
one statement is enough |
|||
<syntaxhighlight lang="rexx">_+=1</syntaxhighlight> |
|||
<pre> |
|||
H:\>regina crash |
|||
1 +++ _+=1 |
|||
Error 41 running "H:\crash.rex", line 1: Bad arithmetic conversion |
|||
Error 41.1: Non-numeric value ("_") to left of arithmetic operation "+=" |
|||
H:\>rexx crash |
|||
1 *-* _+=1 |
|||
Error 41 running H:\crash.rex line 1: Bad arithmetic conversion. |
|||
Error 41.1: Nonnumeric value ("_") used in arithmetic operation.</pre> |
|||
===Version 3=== |
|||
even shorter |
|||
<syntaxhighlight lang="rexx">+</syntaxhighlight> |
|||
<pre> |
|||
H:\>rexx crash |
|||
1 *-* + |
|||
Error 35 running H:\crash.rex line 1: Invalid expression. |
|||
Error 35.901: Prefix operator "+" is not followed by an expression term. |
|||
H:\>regina crash |
|||
Error 35 running "H:\crash.rex", line 1: Invalid expression |
|||
Error 35.1: Invalid expression detected at "</pre> |
|||
=={{header|Ring}}== |
|||
<syntaxhighlight lang="ring"> |
|||
try |
|||
see 5/0 |
|||
catch |
|||
see "Catch!" + nl + cCatchError |
|||
done |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Catch! |
|||
Error (R1) : Can't divide by zero ! |
|||
</pre> |
|||
=={{header|Ruby}}== |
|||
<syntaxhighlight lang="ruby"> |
|||
raise |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
1:in `<main>': unhandled exception |
|||
</pre> |
|||
=={{header|Rust}}== |
|||
Rust provides the panic! macro for stopping execution when encountering unrecoverable errors. This results in a crash, rather than a normal exit. |
|||
<syntaxhighlight lang="rust"> |
|||
fn main(){panic!("");} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
thread 'main' panicked at '', src\main.rs:1:12 |
|||
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace |
|||
</pre> |
|||
=={{header|Swift}}== |
|||
Swift provides a built-in function whose sole purpose is to stop execution in the event of unrecoverable errors. This is different from the standard exit function, as it causes an actual trap (i.e. program crash). As such, it uses the special return value of <code>Never</code>, which allows it to be used in returns that normally expect another type. |
|||
<syntaxhighlight lang="swift">fatalError("You've met with a terrible fate, haven't you?")</syntaxhighlight> |
|||
{{out}} |
|||
<pre>$ ./.build/x86_64-apple-macosx/release/Runner |
|||
Runner/main.swift:11: Fatal error: You've met with a terrible fate, haven't you? |
|||
Illegal instruction: 4</pre> |
|||
=={{header|Tiny BASIC}}== |
|||
<syntaxhighlight lang="tinybasic">0 gosub 0</syntaxhighlight> |
|||
=={{header|V (Vlang)}}== |
|||
<syntaxhighlight lang="v (vlang)">fn main() { panic(0) }</syntaxhighlight> |
|||
=={{header|Wren}}== |
|||
<syntaxhighlight lang="wren">Fiber.abort("")</syntaxhighlight> |
|||
=={{header|XBS}}== |
|||
Calling the error function in the standard library should stop all running code. |
|||
<syntaxhighlight lang="xbs">error("Crashed");</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
{XBS}: CodeError: Crashed |
|||
</pre> |
|||
=={{header|XPL0}}== |
|||
This overflows the stack. It gives a "Segmentation fault" under Raspberry |
|||
Pi OS, and just hangs (in some cases such that Ctrl+Alt+Del doesn't even |
|||
work) under MS-DOS. |
|||
<syntaxhighlight lang="xpl0">proc Recurse; Recurse; Recurse</syntaxhighlight> |
|||
=={{header|Z80 Assembly}}== |
=={{header|Z80 Assembly}}== |
||
{{wont work with|Game Boy}} |
|||
The processor is permanently halted. Strangely enough, this does not work on the Game Boy. Rather, both the <code>HALT</code> instruction <i>and the instruction after it</i> are skipped. |
|||
The CPU will halt and will require a reset. (Earlier there was a mention that the Game Boy is different in this regard - that was an error; it is not.) |
|||
<lang z80>di |
|||
<syntaxhighlight lang="z80">di |
|||
halt</lang> |
|||
halt</syntaxhighlight> |
Latest revision as of 17:09, 20 April 2024
Create a program that crashes as soon as possible, with as few lines of code as possible. Be smart and don't damage your computer, ok?
You are encouraged to solve this task according to the task description, using any language you may know.
- Task
The code should be syntactically valid. It should be possible to insert [a subset of] your submission into another program, presumably to help debug it, or perhaps for use when an internal corruption has been detected and it would be dangerous and irresponsible to continue.
- References
- Related Tasks
11l
assert(0B)
6502 Assembly
Upon executing this byte as code, the processor will halt. No interrupts can occur either. This does not occur on 65c02-based hardware such as the Apple II or Atari Lynx.
db $02
This version works on all 6502 models:
forever:
jmp forever
This code is often written as JMP $
which means the same thing. (In addition to the hexadecimal token, $ can refer to the value of the program counter at that instruction's address.
8080 Assembly
di
hlt
8086 Assembly
Disabling interrupts prior to a HLT
command will cause the CPU to wait forever.
cli
hlt
68000 Assembly
If interrupts are disabled, a jump instruction that jumps to itself will do just fine.
jmp * ;many assemblers allow * or $ to represent the address of this line of code.
Ada
procedure Halt_And_Catch_Fire is
begin
raise Program_Error with "Halt and catch fire";
end Halt_And_Catch_Fire;
- Output:
raised PROGRAM_ERROR : Halt and catch fire
ALGOL 68
This program will crash immediately on startup.
( print( ( 1 OVER 0 ) ) )
ALGOL W
This won't halt the CPU but the program will crash immediately on startup.
assert false.
Applesoft BASIC
The $02 op code won't crash the Apple IIGS. Here is the 6502 Assembly for a relocatable infinite loop consisting of 3 bytes:
:B8 CLV :50 FE BVC {-02}
This is a one-liner that embeds the 3 bytes in a string, and calls the code contained within the string.
HCF$ = CHR$ (184) + "P" + CHR$ (254): CALL PEEK ( PEEK (131) + PEEK (132) * 256 + 1) + PEEK ( PEEK (131) + PEEK (132) * 256 + 2) * 256
Arturo
0/0
- Output:
>> Runtime | File: halt and catch fire.art error | Line: 1 | | uncaught system exception: | division by zero
AWK
# syntax: GAWK -f HALT_AND_CATCH_FIRE.AWK
#
# This won't halt the CPU but the program will crash immediately on startup
# with "error: division by zero attempted".
BEGIN { 1/0 }
#
# This will heat up the CPU, don't think it will catch on fire.
BEGIN { while(1) {} }
#
# Under TAWK 5.0 using AWKW will immediately abort.
BEGIN { abort(1) }
- Output:
gawk: C:\AWK\HALT_AND_CATCH_FIRE.AWK:5: error: division by zero attempted
Binary Lambda Calculus
BLC forces normal programs to start with a closed lambda term, by mapping free variables to the divergent Omega = (\x.x x)(\x.x x)
, the lambda calculus equivalent of an infinite loop. That makes the following 2-bit BLC program the smallest to catch fire:
10
BQN
The easiest way to terminate with an error is using assert (!
):
! "Insert value that is not 1"
"Error Message" ! "Value that is not 1, again"
Other runtime errors are possible, but not as easy to use.
Bruijn
Bruijn does not have runtime errors. For debugging you can either write tests (which are run before evaluating main) or use tactical infinite loops:
:test ([[0]]) ([[1]])
main [[0 0] [0 0]]
C
int main(){int a=0, b=0, c=a/b;}
C++
Use an unhandled exception to crash the program.
#include <stdexcept>
int main()
{
throw std::runtime_error("boom");
}
- Output:
terminate called after throwing an instance of 'std::runtime_error' what(): boom
The output depends on the compiler and platform but should be similar.
C#
This throws a DivideByZeroException at runtime.
int a=0,b=1/a;
This will throw a compile-time exception, so technically not a valid solution.
int a=1/0;
This one-liner also works
throw new System.Exception();
Computer/zero Assembly
STP
Crystal
raise "fire"
Delphi
The program uses Delphi's builtin exception processing to throw an exception. Uncaught exceptions abort a program
procedure HaltAndCatchFire;
begin
raise Exception.Create('Burning to the ground');
end;
- Output:
EasyLang
a[] = [ ]
print a[1]
- Output:
*** ERROR: index out of bounds
F#
0/0
- Output:
[ERROR] FATAL UNHANDLED EXCEPTION: System.DivideByZeroException: Attempted to divide by zero. exit status 1
Factor
REPL
Causing a stack underflow is trivial; just call any word that expects arguments with an empty data stack.
+
- Output:
Data stack underflow
script
This crashes because Factor expects the data stack to be empty at the end of a program. However, it is not here.
1
- Output:
Quotation's stack effect does not match call site quot [ 1 ] call-site ( -- ) (U) Quotation: [ c-to-factor => ] Word: c-to-factor (U) Quotation: [ [ (get-catchstack) push ] dip call => (get-catchstack) pop* ] (O) Word: command-line-startup (O) Word: run (O) Word: load-vocab (O) Method: M\ vocab (require) (O) Word: load-source (O) Word: wrong-values (O) Method: M\ object throw (U) Quotation: [ OBJ-CURRENT-THREAD special-object error-thread set-global current-continuation => error-continuation set-global [ original-error set-global ] [ rethrow ] bi
deployed
When deploying as a standalone executable, a main word and vocabulary must be declared. The stack effect checker must be satisfied, so we can't rely on either of the tricks used before. Therefore die
is called instead.
USE: kernel IN: a : b ( -- ) die ; MAIN: b
- Output:
You have triggered a bug in Factor. Please report. critical_error: The die word was called by the library.: 0 Starting low level debugger... Basic commands: q ^Z -- quit Factor c -- continue executing Factor - NOT SAFE t -- throw exception in Factor - NOT SAFE .s .r .c -- print data, retain, call stacks help -- full help, including advanced commands >
FALSE
Any function with the exception of ^
(read from stdin) or ß
(flush stdin) will cause a stack underflow.
.
Alternatively, the FALSE interpreter expects the stack to be empty at the end of the program's execution, and so leaving a value on the stack is also a valid strategy for crashing the program.
0
Fortran
PROGRAM A
CALL ABORT
END
Fermat
Defines, then calls, a function with no parameters that calls itself. A segfault occurs.
Func S=S. S;
This alternative is five bytes longer but crashes more thoroughly; after a warning about end of line inside a string literal it locks my computer up for a good 2-3 minutes before exiting to the command prompt.
while 1 do !' od;
FreeBASIC
Instant segfault.
poke 0,0
This alternative crashes the compiler.
#define A() B()
#define B() A()
A()
GDScript
An empty script will run and immediately error due to not inheriting from Node:
Script inherits from native type 'RefCounted', so it can't be assigned to an object of type: 'Node'
A script with zero warnings:
extends Node
func _init():$a.a()
E 0:00:00:0321 halt_and_catch_fire.gd:2 @ _init(): Node not found: "a" (relative to "Node"). <C++ Error> Method/function failed. Returning: nullptr <C++ Source> scene/main/node.cpp:1364 @ get_node() <Stack Trace> halt_and_catch_fire.gd:2 @ _init()
This attempts to call a method on a nonexistent child node (just accessing without calling will produce a warning Standalone expression (the line has no effect).
Go
This wouldn't survive go fmt which would stretch it out to 5 lines. However, that's not compulsory and the task says do it in as few lines as possible.
package main; import "fmt"; func main(){a, b := 0, 0; fmt.Println(a/b)}
An alternative shorter line would be:
package main; func main(){panic(0)}
GW-BASIC
0 gosub 0
Hare
export fn main() void = abort();
Haskell
An alternative to the following is to use undefined.
main = error "Instant crash"
J
(1e6$a.) memw (mema 1),0 1e6
In other words: allocate one byte of memory and write 1e6 bytes starting at that address.
It's probably more effective to use
exit 0
-- this approach would eliminate dependence on a variety of implementation details.
Java
public final class HaltAndCatchFire {
public static void main(String[] aArgs) {
// Any one of the lines below, when uncommented, will cause a program halt.
// throw new AssertionError("Stop now!");
// System.out.println(0/0);
// Runtime.getRuntime().exit(1);
}
}
jq
Also works with gojq, the Go implementation of jq
The polite way to halt a running jq program is to use `error` or `halt_error`, both of which come in two flavors. For example:
"whoops" | error
or
0 | error("whoops")
It is worth noting that the text of a run-time error can be captured using `error/1`, e.g.
$ jq -n '0 as $x | try (1/$x) catch error("The error text is: \(.)")' jq: error (at <unknown>): The error text is: number (1) and number (0) cannot be divided because the divisor is zero
"Catching fire" is not so easily done.
Julia
To crash the running program:
@assert false "Halt and catch fire."
- Output:
ERROR: AssertionError: Halt and catch fire.
To crash the LLVM virtual machine running Julia with Exception: EXCEPTION_ILLEGAL_INSTRUCTION:
unsafe_load(convert(Ptr{Int}, C_NULL))
Liberty BASIC
This is just one possibility.
Let
Lua
Tricks could be used to shorten this, particularly from interactive REPL, where -_
would be enough (i.e., attempt arithmetic on a nil global), or from a file _()
would be enough (i.e., attempt to call a nil global). This instead focuses on the "be useful elsewhere" aspect of the task, because both seem short-enough as-is:
error(1)
- Output:
1 stack traceback: [C]: in function 'error' stdin:1: in main chunk [C]: in ?
Or:
assert(false)
- Output:
stdin:1: assertion failed! stack traceback: [C]: in function 'assert' stdin:1: in main chunk [C]: in ?
Mathematica/Wolfram Language
Abort[]
Nim
One possibility:
assert false
Another solution with the same number of characters (we could also use mod
instead of div
):
echo 1 div 0
But the shortest solution may be:
assert 1==0
Pascal
Do an illegal memory access at $0
begin pByte($0)^ := 0 end.
- Output:
Runtime error 216 at $0000000000401098
Perl
This is not a syntax error, it is a fatal run time error. See "perldoc perldiag".
&a
- Output:
Undefined subroutine &main::a called at line 1.
PL/M
This will terminate the program by restarting CP/M.
100H: GOTO 0; EOF
Phix
I normally and quite often just use this:
?9/0
The ? means print and/but obviously the 9/0 triggers a fatal error before it gets that far.
- Output:
C:\Program Files (x86)\Phix\test.exw:1 attempt to divide by 0 Global & Local Variables --> see C:\Program Files (x86)\Phix\ex.err Press Enter...
Alternatives include crash("some message") which produces similar output, and abort(n) which is somewhat quieter with abort(0) meaning (immediately) terminate normally without an error. All of those can be caught by try/catch: should you want to get properly brutal and defeat any active exception handler you can/must resort to inline assembly:
try #ilASM{ [PE32] push 1 -- uExitCode call "kernel32","ExitProcess" [PE64] sub rsp,8*5 mov rcx,1 -- uExitCode call "kernel32","ExitProcess" [ELF32] xor ebx, ebx mov eax, 1 -- SYSCALL_EXIT int 0x80 [ELF64] mov rax,231 -- sys_exit_group(rdi=int error_code) xor rdi,rdi syscall } catch e ?e end try
No output, the try/catch is just for show. ExitProcess/sys_exit are the only non-catchable things I know of, apart from a few other deliberates such as quitting the debugger, and aside from being technically difficult to catch it seems reasonable to classify them as direct actions rather than errors, and that way excuse the non-catchableness.
(I suppose [ok, actually know that] you could also write inline assembly that fubars the call stack to [effectively or quite deliberately] disable any active exception handler[s])
Python
0/0
- Output:
Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: division by zero
Quackery
Ripping lumps out of core definitions will do the trick.
> quackery Welcome to Quackery. Enter "leave" to leave the shell. Building extensions. /O> ' tuck take ... Quackery system damage detected. Python reported: maximum recursion depth exceeded
Raku
++8
Syntactically: Valid.
Semantically: Change the mathematical concept of 8 to 9, either in your whole computer, or maybe the whole universe.
Fails with this run-time error:
- Output:
Cannot resolve caller prefix:<++>(Int:D); the following candidates match the type but require mutable arguments: (Mu:D $a is rw) (Int:D $a is rw --> Int:D) The following do not match for other reasons: (Bool $a is rw) (Mu:U $a is rw) (Num:D $a is rw) (Num:U $a is rw) (int $a is rw --> int) (num $a is rw --> num) in block <unit> at -e line 1
Alternately, and perhaps more community condoned, to end the program as soon as possible without trying to change the Laws of the Universe, you could just enter:
die
- In REPL:
Died in block <unit> at <unknown file> line 1
Same character count, exits the program as soon as possible (though trappable if desired through the exception system,) and it looks more like an intentional act rather than a typo. Plus, you can add a message that will be added when it dies to explain why.
Here is a silly alternative : A standalone Unicode counterpart for the yada yada yada operator takes up 3 code units but visually just a single codepoint,
- Output:
cat test.raku ; wc test.raku … 1 1 4 test.raku raku -c test.raku ; echo $? Syntax OK 0 raku test.raku ; echo $? Stub code executed in block <unit> at test.raku line 1 1
However when I tried to combine all to test against the Test module, the last one somehow lived through an EVAL,
use Test;
dies-ok { ++8 };
dies-ok { die };
dies-ok { … };
eval-dies-ok '++8';
eval-dies-ok 'die';
eval-dies-ok '…' ;
- Output:
ok 1 - ok 2 - ok 3 - ok 4 - ok 5 - not ok 6 - # Failed test at all.raku line 11
so it is indeed at one's discretion whether this one is qualified as a crasher.
REXX
Version 1
_=1;_+=
There is no output shown in the DOS window. This REXX program (using Regina REXX) also crashes the Microsoft DOS window (application). ooRexx shows this: H:\>rexx c2 1 *-* _+= Error 35 running H:\c2.rex line 1: Invalid expression. Error 35.918: Missing expression following assignment instruction.
Version 2
one statement is enough
_+=1
H:\>regina crash 1 +++ _+=1 Error 41 running "H:\crash.rex", line 1: Bad arithmetic conversion Error 41.1: Non-numeric value ("_") to left of arithmetic operation "+=" H:\>rexx crash 1 *-* _+=1 Error 41 running H:\crash.rex line 1: Bad arithmetic conversion. Error 41.1: Nonnumeric value ("_") used in arithmetic operation.
Version 3
even shorter
+
H:\>rexx crash 1 *-* + Error 35 running H:\crash.rex line 1: Invalid expression. Error 35.901: Prefix operator "+" is not followed by an expression term. H:\>regina crash Error 35 running "H:\crash.rex", line 1: Invalid expression Error 35.1: Invalid expression detected at "
Ring
try
see 5/0
catch
see "Catch!" + nl + cCatchError
done
- Output:
Catch! Error (R1) : Can't divide by zero !
Ruby
raise
- Output:
1:in `<main>': unhandled exception
Rust
Rust provides the panic! macro for stopping execution when encountering unrecoverable errors. This results in a crash, rather than a normal exit.
fn main(){panic!("");}
- Output:
thread 'main' panicked at '', src\main.rs:1:12 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Swift
Swift provides a built-in function whose sole purpose is to stop execution in the event of unrecoverable errors. This is different from the standard exit function, as it causes an actual trap (i.e. program crash). As such, it uses the special return value of Never
, which allows it to be used in returns that normally expect another type.
fatalError("You've met with a terrible fate, haven't you?")
- Output:
$ ./.build/x86_64-apple-macosx/release/Runner Runner/main.swift:11: Fatal error: You've met with a terrible fate, haven't you? Illegal instruction: 4
Tiny BASIC
0 gosub 0
V (Vlang)
fn main() { panic(0) }
Wren
Fiber.abort("")
XBS
Calling the error function in the standard library should stop all running code.
error("Crashed");
- Output:
{XBS}: CodeError: Crashed
XPL0
This overflows the stack. It gives a "Segmentation fault" under Raspberry Pi OS, and just hangs (in some cases such that Ctrl+Alt+Del doesn't even work) under MS-DOS.
proc Recurse; Recurse; Recurse
Z80 Assembly
The CPU will halt and will require a reset. (Earlier there was a mention that the Game Boy is different in this regard - that was an error; it is not.)
di
halt