Halt and catch fire: Difference between revisions

m
syntax highlighting fixup automation
(Added Hare)
m (syntax highlighting fixup automation)
Line 19:
{{trans|Nim}}
 
<syntaxhighlight lang ="11l">assert(0B)</langsyntaxhighlight>
 
=={{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.
<syntaxhighlight lang ="6502asm"> db $02</langsyntaxhighlight>
 
This version works on all 6502 models:
<langsyntaxhighlight lang="6502asm">forever:
jmp forever</langsyntaxhighlight>
 
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.
Line 33:
=={{header|8080 Assembly}}==
{{trans|Z80 Assembly}}
<langsyntaxhighlight lang="8080asm">di
hlt</langsyntaxhighlight>
 
=={{header|8086 Assembly}}==
{{trans|Z80 Assembly}}
Disabling interrupts prior to a <code>HLT</code> command will cause the CPU to wait forever.
<langsyntaxhighlight lang="asm">cli
hlt</langsyntaxhighlight>
 
=={{header|68000 Assembly}}==
If interrupts are disabled, a jump instruction that jumps to itself will do just fine.
<langsyntaxhighlight lang="68000devpac">jmp * ;many assemblers allow * or $ to represent the address of this line of code.</langsyntaxhighlight>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">procedure Halt_And_Catch_Fire is
begin
raise Program_Error with "Halt and catch fire";
end Halt_And_Catch_Fire;</langsyntaxhighlight>
{{out}}
<pre>raised PROGRAM_ERROR : Halt and catch fire
Line 57:
=={{header|ALGOL 68}}==
This program will crash immediately on startup.
<langsyntaxhighlight lang="algol68">( print( ( 1 OVER 0 ) ) )</langsyntaxhighlight>
 
=={{header|ALGOL W}}==
This won't halt the CPU but the program will crash immediately on startup.
<syntaxhighlight lang ="algolw">assert false.</langsyntaxhighlight>
 
=={{header|Applesoft BASIC}}==
Line 69:
: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.
<langsyntaxhighlight lang="gwbasic">HCF$ = CHR$ (184) + "P" + CHR$ (254): CALL PEEK ( PEEK (131) + PEEK (132) * 256 + 1) + PEEK ( PEEK (131) + PEEK (132) * 256 + 2) * 256</langsyntaxhighlight>
=={{header|Arturo}}==
 
<syntaxhighlight lang ="rebol">0/0</langsyntaxhighlight>
 
{{out}}
Line 83:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f HALT_AND_CATCH_FIRE.AWK
#
Line 95:
# Under TAWK 5.0 using AWKW will immediately abort.
BEGIN { abort(1) }
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 102:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">int main(){int a=0, b=0, c=a/b;}</langsyntaxhighlight>
 
=={{header|C++}}==
Use an unhandled exception to crash the program.
<langsyntaxhighlight lang="cpp">#include <stdexcept>
int main()
{
throw std::runtime_error("boom");
}</langsyntaxhighlight>
{{out}}
<pre>
Line 121:
{{works with|C sharp|9}}
This throws a DivideByZeroException at runtime.<br/>
<langsyntaxhighlight lang="csharp">int a=0,b=1/a;</langsyntaxhighlight>
This will throw a compile-time exception, so technically not a valid solution.
<langsyntaxhighlight lang="csharp">int a=1/0;</langsyntaxhighlight>
This one-liner also works
<syntaxhighlight lang ="csharp">throw new System.Exception();</langsyntaxhighlight>
 
=={{header|Computer/zero Assembly}}==
<syntaxhighlight lang ="6502asm">STP</langsyntaxhighlight>
=={{header|Crystal}}==
<langsyntaxhighlight lang="crystal">raise "fire"</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
0/0
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 145:
===REPL===
Causing a stack underflow is trivial; just call any word that expects arguments with an empty data stack.
<syntaxhighlight lang ="factor">+</langsyntaxhighlight>
 
{{out}}
Line 154:
===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</langsyntaxhighlight>
 
{{out}}
Line 180:
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.
 
<langsyntaxhighlight lang="factor">USE: kernel IN: a : b ( -- ) die ; MAIN: b</langsyntaxhighlight>
{{out}}
<pre>
Line 198:
=={{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">.</langsyntaxhighlight>
 
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</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|77,90,95,...}}
<langsyntaxhighlight lang="fortran"> PROGRAM A
CALL ABORT
END</langsyntaxhighlight>
 
=={{header|Fermat}}==
Defines, then calls, a function with no parameters that calls itself. A segfault occurs.
<langsyntaxhighlight lang="fermat">Func S=S. S;</langsyntaxhighlight>
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.
<langsyntaxhighlight lang="fermat">while 1 do !' od;</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
Instant segfault.
<syntaxhighlight lang ="freebasic">poke 0,0</langsyntaxhighlight>
This alternative crashes the compiler.
<langsyntaxhighlight lang="freebasic">#define A() B()
#define B() A()
A()</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="go">package main; import "fmt"; func main(){a, b := 0, 0; fmt.Println(a/b)}</langsyntaxhighlight>
<br>
An alternative shorter line would be:
<langsyntaxhighlight lang="go">package main; func main(){panic(0)}</langsyntaxhighlight>
 
=={{header|GW-BASIC}}==
<syntaxhighlight lang ="gwbasic">0 gosub 0</langsyntaxhighlight>
 
=={{header|Hare}}==
<langsyntaxhighlight lang="hare">export fn main() void = abort();</langsyntaxhighlight>
 
=={{header|Haskell}}==
An alternative to the following is to use ''undefined''.
<langsyntaxhighlight lang="haskell">main = error "Instant crash"</langsyntaxhighlight>
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j"> (1e6$a.) memw (mema 1),0 1e6</langsyntaxhighlight>
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="j"> exit 0</langsyntaxhighlight> -- this approach would eliminate dependence on a variety of implementation details.
 
=={{header|Julia}}==
To crash the running program:
<langsyntaxhighlight lang="julia">@assert false "Halt and catch fire."</langsyntaxhighlight>{{out}}<pre>ERROR: AssertionError: Halt and catch fire.</pre>
To crash the LLVM virtual machine running Julia with Exception: EXCEPTION_ILLEGAL_INSTRUCTION:
<langsyntaxhighlight lang="julia">unsafe_load(convert(Ptr{Int}, C_NULL))</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
This is just one possibility.
<syntaxhighlight lang ="lb">Let</langsyntaxhighlight>
 
=={{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)</langsyntaxhighlight>
{{out}}
<pre>1
Line 266:
[C]: in ?</pre>
Or:
<syntaxhighlight lang ="lua">assert(false)</langsyntaxhighlight>
{{out}}
<pre>stdin:1: assertion failed!
Line 275:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang Mathematica="mathematica">Abort[]</langsyntaxhighlight>
 
=={{header|Nim}}==
One possibility:
<syntaxhighlight lang Nim="nim">assert false</langsyntaxhighlight>
 
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</langsyntaxhighlight>
 
=={{header|Pascal}}==
{{Works with|Free Pascal}} Do an illegal memory access at $0
<langsyntaxhighlight lang="pascal">begin pByte($0)^ := 0 end.</langsyntaxhighlight>
{{out}}
<pre>Runtime error 216 at $0000000000401098</pre>
Line 292:
=={{header|Perl}}==
This is not a syntax error, it is a fatal run time error. See "perldoc perldiag".
<syntaxhighlight lang ="perl">&a</langsyntaxhighlight>
{{out}}
<pre>Undefined subroutine &main::a called at line 1.</pre>
Line 298:
=={{header|PL/M}}==
This will terminate the program by restarting CP/M.
<syntaxhighlight lang ="plm">100H: GOTO 0; EOF</langsyntaxhighlight>
 
=={{header|Phix}}==
I normally and quite often just use this:
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #0000FF;">?</span><span style="color: #000000;">9</span><span style="color: #0000FF;">/</span><span style="color: #000000;">0</span>
<!--</langsyntaxhighlight>-->
The ? means print and/but obviously the 9/0 triggers a fatal error before it gets that far.
{{out}}
Line 317:
</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:
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">try</span>
#ilASM{
Line 339:
<span style="color: #0000FF;">?</span><span style="color: #000000;">e</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">try</span>
<!--</langsyntaxhighlight>-->
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="python">0/0</langsyntaxhighlight>
{{out}}
<pre>Traceback (most recent call last):
Line 370:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>++8</langsyntaxhighlight>
Syntactically: Valid.
 
Line 392:
 
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" perl6line>die</langsyntaxhighlight>
{{out|In REPL}}
<pre>Died
Line 417:
However when I tried to combine all to test against the Test module, the last one somehow lived through an EVAL,
 
<syntaxhighlight lang="raku" perl6line>use Test;
 
dies-ok { ++8 };
Line 425:
eval-dies-ok '++8';
eval-dies-ok 'die';
eval-dies-ok '…' ;</langsyntaxhighlight>
 
{{out}}
Line 440:
=={{header|REXX}}==
===Version 1===
<langsyntaxhighlight lang="rexx">_=1;_+=</langsyntaxhighlight>
<pre>
There is no output shown in the DOS window. &nbsp; This REXX program (using Regina REXX) also
Line 451:
===Version 2===
one statement is enough
<langsyntaxhighlight lang="rexx">_+=1</langsyntaxhighlight>
<pre>
H:\>regina crash
Line 464:
===Version 3===
even shorter
<syntaxhighlight lang ="rexx">+</langsyntaxhighlight>
<pre>
H:\>rexx crash
Line 476:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
try
see 5/0
Line 482:
see "Catch!" + nl + cCatchError
done
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 489:
</pre>
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">
raise
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 499:
=={{header|Rust}}==
Rust provides the panic! macro for stopping execution when encountering unrecoverable errors. This results in a crash, rather than a normal exit.
<langsyntaxhighlight lang="rust">
fn main(){panic!("");}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 512:
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.
 
<langsyntaxhighlight lang="swift">fatalError("You've met with a terrible fate, haven't you?")</langsyntaxhighlight>
 
{{out}}
Line 521:
 
=={{header|Tiny BASIC}}==
<syntaxhighlight lang ="tinybasic">0 gosub 0</langsyntaxhighlight>
 
=={{header|Vlang}}==
<langsyntaxhighlight lang="vlang">fn main() { panic(0) }</langsyntaxhighlight>
 
=={{header|Wren}}==
<langsyntaxhighlight lang="ecmascript">Fiber.abort("")</langsyntaxhighlight>
 
=={{header|XBS}}==
Calling the error function in the standard library should stop all running code.
<langsyntaxhighlight lang="xbs">error("Crashed");</langsyntaxhighlight>
{{out}}
<pre>
Line 541:
Pi OS, and just hangs (in some cases such that Ctrl+Alt+Del doesn't even
work) under MS-DOS.
<syntaxhighlight lang XPL0="xpl0">proc Recurse; Recurse; Recurse</langsyntaxhighlight>
 
=={{header|Z80 Assembly}}==
{{wont work with|Game Boy}}
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.)
<langsyntaxhighlight lang="z80">di
halt</langsyntaxhighlight>
10,327

edits