Halt and catch fire

From Rosetta Code
Revision as of 20:04, 17 January 2022 by rosettacode>Nukesrus21 (Updated Liberty BASIC's example)
Halt and catch fire
You are encouraged to solve this task according to the task description, using any language you may know.

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.


Related Tasks


Translation of: Nim

<lang 11l>assert(0B)</lang>

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. <lang 6502asm> db $02</lang>

This version works on all 6502 models: <lang 6502asm>forever: jmp forever</lang>

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

Translation of: Z80 Assembly

<lang 8080asm>di hlt</lang>

8086 Assembly

Translation of: Z80 Assembly

Disabling interrupts prior to a HLT command will cause the CPU to wait forever. <lang asm>cli hlt</lang>

68000 Assembly

The 68000 can only read words or longs at even addresses. Attempting to do so at an odd address will crash the CPU. <lang 68000devpac>CrashBandicoot equ $100001 LEA CrashBandicoot,A0 MOVE.W (A0),D0</lang>


<lang Ada>procedure Halt_And_Catch_Fire is begin

  raise Program_Error with "Halt and catch fire";

end Halt_And_Catch_Fire;</lang>

raised PROGRAM_ERROR : Halt and catch fire


This program will crash immediately on startup. <lang algol68>( print( ( 1 OVER 0 ) ) )</lang>


This won't halt the CPU but the program will crash immediately on startup. <lang algolw>assert false.</lang>


<lang AWK>

  2. This won't halt the CPU but the program will crash immediately on startup
  3. with "error: division by zero attempted".

BEGIN { 1/0 }

  1. This will heat up the CPU, don't think it will catch on fire.

BEGIN { while(1) {} }

  1. Under TAWK 5.0 using AWKW will immediately abort.

BEGIN { abort(1) } </lang>

gawk: C:\AWK\HALT_AND_CATCH_FIRE.AWK:5: error: division by zero attempted


<lang c>int main(){int a=0, b=0, c=a/b;}</lang>


Use an unhandled exception to crash the program. <lang cpp>#include <stdexcept> int main() {

   throw std::runtime_error("boom");


terminate called after throwing an instance of 'std::runtime_error'
  what():  boom

The output depends on the compiler and platform but should be similar.


Works with: C sharp version 9

This throws a DivideByZeroException at runtime.
<lang csharp>int a=0,b=1/a;</lang> This will throw a compile-time exception, so technically not a valid solution. <lang csharp>int a=1/0;</lang> This one-liner also works <lang csharp>throw new System.Exception();</lang>


<lang fsharp> 0/0 </lang>

[ERROR] FATAL UNHANDLED EXCEPTION: System.DivideByZeroException: Attempted to divide by zero.
exit status 1



Causing a stack underflow is trivial; just call any word that expects arguments with an empty data stack. <lang factor>+</lang>

Data stack underflow


This crashes because Factor expects the data stack to be empty at the end of a program. However, it is not here. <lang factor>1</lang>

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


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.

<lang factor>USE: kernel IN: a : b ( -- ) die ; MAIN: b</lang>

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



Defines, then calls, a function with no parameters that calls itself. A segfault occurs. <lang fermat>Func S=S. S;</lang> 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. <lang fermat>while 1 do !' od;</lang>


Instant segfault. <lang freebasic>poke 0,0</lang> This alternative crashes the compiler. <lang freebasic>#define A() B()

  1. define B() A()



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. <lang go>package main; import "fmt"; func main(){a, b := 0, 0; fmt.Println(a/b)}</lang>
An alternative shorter line would be: <lang go>package main; func main(){panic(0)}</lang>


<lang gwbasic>0 gosub 0</lang>


An alternative to the following is to use undefined. <lang haskell>main = error "Instant crash"</lang>


To crash the running program:

<lang julia>@assert false "Halt and catch fire."</lang>
ERROR: AssertionError: Halt and catch fire.

To crash the LLVM virtual machine running Julia with Exception: EXCEPTION_ILLEGAL_INSTRUCTION: <lang julia>unsafe_load(convert(Ptr{Int}, C_NULL))</lang>

Liberty BASIC

This is just one possibility. <lang lb>Let</lang>


One possibility: <lang Nim>assert false</lang>

Another solution with the same number of characters (we could also use mod instead of div): <lang>echo 1 div 0</lang>


Works with: Free Pascal
Do an illegal memory access at $0

<lang pascal>begin pByte($0)^ := 0 end.</lang>

Runtime error 216 at $0000000000401098


This is not a syntax error, it is a fatal run time error. See "perldoc perldiag". <lang perl>&a</lang>

Undefined subroutine &main::a called at line 1.


This will terminate the program by restarting CP/M. <lang plm>100H: GOTO 0; EOF</lang>


I normally and quite often just use this:


The ? means print and/but obviously the 9/0 triggers a fatal error before it gets that far.

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:

           push 1 -- uExitCode
           call "kernel32","ExitProcess"
           sub rsp,8*5
           mov rcx,1 -- uExitCode
           call "kernel32","ExitProcess"
           xor ebx, ebx 
           mov eax, 1  -- SYSCALL_EXIT 
           int 0x80 
           mov rax,231 -- sys_exit_group(rdi=int error_code) 
           xor rdi,rdi
catch 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])


<lang Python>0/0</lang>

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero


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


<lang perl6>++8</lang> 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:

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: <lang perl6>die</lang>

  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,

cat test.raku ; wc test.raku
1 1 4 test.raku

raku -c test.raku ; echo $?
Syntax OK

raku test.raku ; echo $?
Stub code executed
  in block <unit> at test.raku line 1


However when I tried to combine all to test against the Test module, the last one somehow lived through an EVAL,

<lang perl6>use Test;

dies-ok { ++8 }; dies-ok { die }; dies-ok { … };

eval-dies-ok '++8'; eval-dies-ok 'die'; eval-dies-ok '…' ;</lang>

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.


Version 1

<lang rexx>_=1;_+=</lang>

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 <lang rexx>_+=1</lang>

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 <lang rexx>+</lang>

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 "


<lang ring> try

  see 5/0


  see "Catch!" + nl + cCatchError

done </lang>

Error (R1) : Can't divide by zero !


<lang ruby> raise </lang>

1:in `<main>': unhandled exception


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.

<lang swift>fatalError("You've met with a terrible fate, haven't you?")</lang>

$ ./.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


<lang tinybasic>0 gosub 0</lang>


<lang ecmascript>Fiber.abort("")</lang>


Calling the error function in the standard library should stop all running code. <lang xbs>error("Crashed");</lang>

{XBS}: CodeError: Crashed


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. <lang XPL0>proc Recurse; Recurse; Recurse</lang>

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.) <lang z80>di halt</lang>