Inspired by this default for IO.

The Task

Write a program that, given an input integer x between 0 and 255, crashes with exit code x.

Restrictions

  • You may not call anything which is intended directly for exit code output (System.exit(x), returning from main, etc.). Instead, your program must cause an error or crash which will cause, with a probability of 1, the program to exit with the input integer.
    • In this case, the words "error" and "crash" means that the program caused some fatal unintended exception, wherein a method, operation or otherwise was used incorrectly that caused an irreversible failure.
    • You may not directly cause the error by throwing it directly. You must cause it by using a method, function, or otherwise whose purpose serves a different function (i.e. attempting to execute a file write to a read-only directory).
  • You must have at least exit codes 0 and 1 accounted for in your program.
  • Termination of the process with the use of signals is banned. (Reasoning may be found in this discussion)

Scoring

Your program's score is determined by the number of supported exit codes, where code concision is tiebreaker. The greatest number of supported exit codes wins!

share|improve this question
    
Just to clarify, if I raise an error, that is illegal yes? – Anthony Pham 14 hours ago
    
@AnthonyPham If you directly raise it (throw new Exception() style), that is illegal. If it is the byproduct of misuse of an existing function, then that's fine. – VoteToClose 14 hours ago
    
@VoteToClose I guess I wasn't clear enough. I agree with you, but was suggesting, say square, instead of factorial. But whatever, it doesn't really matter, was just a suggestion – Maltysen 14 hours ago
    
Sushi has an exit code for every possible error. Too bad I haven't published the interpreter yet. – Pavel 14 hours ago
2  
Is accepting input as spelt-out English digits (e.g. one zero zero for 100) acceptable? I have an idea for this challenge, but the language has some fairly unusual ideas about I/O, and this is the most natural input format. – ais523 13 hours ago

13 Answers 13

Bash 4.2 + extras, 20 exit codes

case $1 in
1)man;;
2)grep;;
3)curl /;;
4)sed;;
6)curl _;;
7)curl 0;;
16)man /;;
22)curl -f to/!;;
23)curl to -o/;;
63)curl --max-filesize 1 to;;
124)timeout .01 w;;
126)/;;
127)-;;
132)tcc -run <(echo main=6\;);;
133)tcc -run <(echo main=204\;);;
136)$[2**63%-1];;
137){0..1000000000};;
139)tcc -run <(echo main=0\;);;
255)exit _
esac

The last one's borderline, but since exit underscore fails because of its non-numeric argument, I think it should count.

I will golf this later.

Verification

$ for n in {0..255}; do bash exit.sh $n ; (($? == $n)) && echo $n >&0; done &>-
0
1
2
3
4
6
7
16
22
23
63
124
126
127
132
133
136
137
139
255
share|improve this answer
    
@VoteToClose At least on Fedora, grep isn't packaged with coreutils. – Dennis 13 hours ago
    
@Pavel Added an explanation. – Dennis 13 hours ago
    
You support 3, right? – SeeOneRhino 11 hours ago
    
@SeeOneRhino Right. I'll just remove that part; the exit codes are listed below anyway. – Dennis 11 hours ago
    
@Qwerp-Derp Yes, that was a typo. – Dennis 10 hours ago

Unix Shell (+ncurses), 36 bytes, 256 exit codes

Golfed

seq -f "setf %0.f" $[252+$1]|tput -S

If the -S option is used, tput checks for errors from each line, and if any errors are found, will set the exit code to 4 plus the number of lines with errors. If no errors are found, the exit code is 0. No indication of which line failed can be given so exit code 1 will never appear. Exit codes 2, 3, and 4 retain their usual interpretation.

Once the tput exit code goes over 255, it just overflows, so 253 (errors on input) will result in the exit code of 1 e.t.c., thus yielding the desired exit status for the whole range of inputs.

Note: whether tput will fail on not, when setting a particular capability, depends on the terminal type, I've used: xterm with 256 colors

Try It Online !

share|improve this answer
    
Wow, that's pretty good. +1 – VoteToClose 3 hours ago
    
over 255, it just overflows, so 253 results in 1 e.t.c. is that a typo? you meant 256? – Federico 1 hour ago
    
@Federico, tput will add +4 to the number of "errors", so 253 errors will result in the exit code of 253+4=>257%256=>1 (I will update the post to make this more clear). – zeppelin 1 hour ago

Excel VBA, 414 514 + 3 Bytes, 13 exit codes

Takes input as a Conditional Compilation Argument, n=[input value] and produces that number's associated error code.

Sub e()
Dim a As Application, b As Byte, s As String, v(), x
Set a=Application
#If n=3 Then
Return
#ElseIf n=5 Then
b=a.CommandBars("")
#ElseIf n=6 Then
b=2^8
#ElseIf n=7 Then
ReDim v(9^9)
#ElseIf n=9 Then
v=Sheets("")
#ElseIf n=10 Then
v=Array(1)
For Each x In v
ReDim v(9)
Next
#ElseIf n=11 Then
b=1/0
#ElseIf n=13 Then
b=""
s="Z"
For b=0To 99
s=s &s
Next
#ElseIf n=20 Then
Resume
#ElseIf n=28 Then
f 256
#ElseIf n=35 Then
Call c
#ElseIf n=92 Then
For Each x In v
ReDim v(9)
Next
#ElseIf n=94 Then
b=Null
#End If
End Sub

Sub f(n):DoEvents:f n-1:End Sub

+3 for n=[Value] conditional compilation call

Handles Inputs where n=

3
5
6
7
9
10
11
13
20
28
35
92
94    

Note: VBA does not have exit codes 0 or 1. I have included the solutions for 3 and 5, which are the two lowest numbered exit codes available to VBA in their place

share|improve this answer
    
I won't disallow this answer, because it's synonymous for me disallowing due to language capability for output types. – VoteToClose 8 hours ago

C (gcc) under bash shell on x86, 230 bytes, 8 exit codes

Newlines added to aid readability.

main(int c, char **v){
int p[2];
switch(atoi(v[1])-128){
case 2:__asm("UD2");
case 5:__asm("int $3");
case 6:abort();
case 8:c/=c-2;
case 11:c=*(int *)c;
case 13:socketpair(1,1,0,p);close(p[1]);write(p[0],v,1);
case 14:alarm(1);sleep(2);
}
}

A feature of the bash shell:

When a command terminates on a fatal signal N, bash uses the value of 128+N as the exit status.

So all we need to do is trigger various signals from within a c program. At this point, I assume simply doing kill(n-128); is banned. So instead we execute code that triggers various signals, which causes the corresponding error codes to be made available at the calling shell.

The exit codes are 0, 130, 133, 134, 136, 139, 141, 142.

Try it online. Expand the "Debug" section to see the return code.

This can certainly be golfed deeper. But I'd be more interested in adding more signals.

share|improve this answer
    
@VoteToClose Do you consider this answer to be legal? I see your note about kill() and signals. I am using signals, but no explicit calls to kill(). – Digital Trauma 13 hours ago

PHP, 15 bytes, 2 exit codes

PHP cannot return anything but 0 or 1, so ...

!$argv[1]?:p();

If command line argument is falsy, it evaluates to 1 and exits with 0. If not, it tries to call a function and exits with <b>Fatal error</b>: Uncaught Error: Call to undefined function p(). Run with -r.

share|improve this answer
    
That's not true. exit() sets an exit status... which you can't use for this challenge, granted. But your code is also invalid. It sets the exit status to 255 – aross 2 hours ago

Perl, 108 bytes, 256 exit codes

This program (ab)uses Test::More module. It tries to open file named "" n times where n is given as command line argument. It fails every time, and each invocation is treated as a test. Test::More return number of failed tests as exit code. plan tests => $ARGV[0]%255 is needed to get exit code 255.

#!/usr/bin/perl
use Test::More;
plan tests => $ARGV[0]%255 if($ARGV[0]>0);
ok(open(F,"")) for (1..$ARGV[0])
share|improve this answer

Befunge-93, 4 bytes, 2 exit codes

&!%@

Supports exit codes 0 and 1.

This relies on the fact that a modulo 0 calculation will typically generate a division by zero error. It is very implementation specific, though, since different interpreters return different values for that error. Fortunately there is at least one interpreter, Befungee, that exits with 0 on success and 1 for any exceptions.

Explanation

&          Read a value from stdin.
 !         Not the value, so 1 becomes 0 and 0 becomes 1.
  %        Calculate the modulo of an implicit 0 with the previous result.
             (i.e. 0%0 if the input was 1, or 0%1 if the input was 0).
   @       Exit cleanly if the modulo operation did not throw an exception.
share|improve this answer

Python 2, 19 Bytes, 2 exit codes

print 1/(input()-1)

If you enter 0, it tries to print 1/-1 which is -1 which is perfectly fine thus exit code 0. If you enter 1, you get 1/0 which raises a ZeroDivisionError in which there is an exit code of 1. With my IDE, there is only 0 and 1 for the exit codes...

Outputs:


enter image description here


enter image description here

share|improve this answer

Scala, 19 bytes, 2 exit codes

if(args(0)=="1")1/0

1/(args(0).toInt-1)

The JVM supports only 1 and 0 as exit codes if you don't call System.exit.

The first program tries to calculate 1/0 if the first argument is 1, which will crash the JVM with an exit code of 1. If the argument is 0, it wil exit succesfully.

The second program to converts the argument to integer, subtracts one and tries to divide 1 by that number. If the argument if 1, it calculates 1/0, so the JVM will crash; if the argument is 0, it calculates 1/-1 and exits.

share|improve this answer

Python 2, 11 bytes, 2 exit codes

1>>-input()
1/~-input()
0<input()<x

Three different 11-byte solutions for three different errors! (Just for fun, this doesn't give points.) By default, Python only has exit codes 0 for successful exit and 1 for error. The successful runs don't output anything.

 1>>-input()

On input 1, gives "ValueError: negative shift count". On input 0, a zero shift count succeeds and gives 1.

1/~-input()

On input 1, gives "ZeroDivisionError: integer division or modulo by zero" due to ~-input(), aka input()-1 being 0. In input 1, 1/-1 gives -1. 0**-input() would also work.

0<input()<x

On input 1, gives "NameError: name 'x' is not defined". On input 0, these first inequality 0<0 is evaluated to False, so the rest is not evaluated and the result is just False.

share|improve this answer

Turtlèd, 4 bytes, 2 exit codes

I don't know if there are any ways to get more exit codes... are there even any more ways in the interpreter language

I found a few four length answers

' ?;

Try it online!

!.(0

Try it online!

?;(*

Try it online!

How these work:

' ?;

in my interpreter, there is a bug feature that causes errors when the grid in memory has more than one line, and has no non-space characters on it. this program erases the * on the origin cell '[space], takes non-negative integer input ? (0 or 1 really), and moves down that many ;, if it is zero, the grid will only have one line and not error, otherwise it will move down and the error will occur

!.(0

parentheses and stuff don't get parsed, they just get executed at run time to mean: "skip to the matching paren, if the cell symbol isn't right". in this program, inputting (!) one causes the program to write it to the cell (.), execute the paren, which checks if the cell symbol is 0, try to skip to the matching paren, but instead throw an error as there is none. if it is zero, it writes it down, checks the parentheses, finds itself on a 0, and then ignores it, and the program finishes

?;(*

has elements of the previous answer, and the first. it takes non-negative integer input, moves down that many, and checks whether the cell is '*', searching for a non existing extra paren if it is not. if the input is 1, it will move off the starting space, and find the cell is a space, and error, if it is zero, it will stay on the start space and ignore the paren.

share|improve this answer

Java, 71 bytes, 2 exit codes

Full program:

interface I{static void main(String[]a){int x=a[0].equals("1")?1/0:1;}}

Function taking an int as an argument:

i->1/(i-1)

Java doesn't seem to like the ? : operator if you you don't assign the result ot a variable. Crashes with exit code 1 while trying to calculate 1/0 when given an input of 1.

share|improve this answer

Javscript (node), 19 bytes, 2 exit codes

Full program:

+process.argv[2]&&a

Function:

f=i=>i&&f(i)

process.argv is an array that contains the path to the node executable, the path to the javascript file executed and the command line arguments. In this case, this will be either "1" or "0". The string is converted to a number with the unary + operator. If the number is zero the lazy && operator won't evaluate the right hand side, if the number is truthy (not zero), the right hand side of && is evaluated and an error is thrown because it's referencing an undefined variable, and the program exists with exit code 1.

The function expects the input as a number. If the input is truthy, the function calls itself, and crashes the node runtime with a stack overflow. If the input is 0, the lazy && operator returns 0 without evaluating the right hand side.

share|improve this answer
    
@VoteToClose Changed it to an undefined variable – corvus_192 4 hours ago
    
That works. :P‮ – VoteToClose 4 hours ago
    
The full program can be shortened to +process.argv[2]&&a. – user2428118 3 hours ago
    
@user2428118 You're right – corvus_192 3 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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