115
\$\begingroup\$

Your challenge: write a "program", for a language of your choice, that causes the compiler/interpreter/runtime to produce error output when compiling/running your program which is identical to your program's source code.

Rules:

  • Your program may be specific to a particular version or implementation of your language's compiler/interpreter/runtime environment. If so, please specify the particulars.
  • Only standard compiler/interpreter/runtime options are permitted. You cannot pass some weird flag to your compiler to get a specific result.
  • The program does not need to be syntactically or semantically valid, but I may give a bounty to the best syntactically valid submission.
  • The program must not produce any output of its own (e.g. by calling a print or output function). All output generated upon attempting to compile/run the program must originate from the compiler/interpreter/runtime.
  • The complete output of the compiler/interpreter/runtime must be exactly identical to your program source code.
  • The compiler/interpreter/runtime must generate at least one error message when invoked with your program.

This is a popularity contest. Most creative answer, as determined by upvotes, wins. If you can give a good case for using a standard loophole, you may do so.

\$\endgroup\$
7
  • 9
    \$\begingroup\$ What is "error output"? And what does it mean to "generate an error message"? More specifically: 1) Does the output have to be to stderr? 2) If the runtime logs an error to syslog and doesn't write anything to stderr, what should be compared to the source of the program? 3) If the runtime throws an exception internally when given an empty program, but requires a flag to actually print the exception and so ends up exiting with a non-zero exit code but no output, has an error message been generated? \$\endgroup\$ Aug 16 '14 at 9:24
  • 1
    \$\begingroup\$ I tried doing this in java and got a p3 oscilator. This was the shortest phase: (Compile from q.java): Error: Could not find or load main class Q \$\endgroup\$ Jun 1 '15 at 12:57
  • 2
    \$\begingroup\$ Ha, because of a syntactic ambiguity in the first sentence, I thought the challenge here was to produce a program which normally produces no output, but if you pass its own code to it as input, it produces an error. \$\endgroup\$ Apr 10 '17 at 6:32
  • 2
    \$\begingroup\$ When reporting an error, APL always prints a customizable error name, optionally an error message, the name of the program that caused the error, the (bracketed) line number where the error occurred, the line of code that caused the error, and a line with a caret indicating where parsing stopped. Any hope for participation here? \$\endgroup\$
    – Adám
    May 7 '17 at 21:50
  • 1
    \$\begingroup\$ @SteveBennett Why haven't you made that challenge yet? \$\endgroup\$ May 28 '17 at 1:53

112 Answers 112

3
+50
\$\begingroup\$

Vyxal, 42 bytes

slice expected at least 1 arguments, got 0

Try it Online!

\$\endgroup\$
2
\$\begingroup\$

Befunge on Wasabi v1.4

There is an interpreter for Befunge called Wasabi, and when an error appears the error message is in the format of a dialog box. This is the program:

Unsupported Command : 'U' at (1, 0). Ignore next syntax error(s)?

To test, download Wasabi v1.4 and insert the above program.

\$\endgroup\$
2
\$\begingroup\$

Factor

No word named "No" found in current vocabulary search path

Guess what it prints?

No word named "No" found in current vocabulary search path
\$\endgroup\$
2
\$\begingroup\$

MATLAB, 66 bytes

Undefined function 'Undefined' for input arguments of type 'char'.

This outputs the same to STDERR (shown in the console in red):

enter image description here

This is actually pretty unknown functionality (and it's rarely useful), which is why the MATLAB submission in the "Hello World!"-challenge may be improved.

This works because MATLAB will automatically interpret a command on the form <word_1 word_2 word_3 word_4>, with no surrounding brackets, as a function on the form:

word_1('word_2')`

That is, it calls a function word_1 and gives the single input argument after the first, and in front of the second space as a string input to that function.

"Real life"-examples that can be used for golfing:

disp Hello   % Shorter than disp('Hello')
Hello

disp Hello World!
Hello

nnz variable
ans =
     8

Note that it will always interpret it as a string, it will not evaluate it:

variable = 3;
nnz variable
ans =
     8
nnz(variable)
ans =
     1
\$\endgroup\$
7
  • \$\begingroup\$ In my current MATLAB version (R2016b), this can be considerably shortened to 43 bytes: Undefined function or variable 'Undefined'.. \$\endgroup\$
    – Sanchises
    Jun 13 '17 at 9:20
  • \$\begingroup\$ Really, it doesn't treat the words after Undefined as string inputs? What happens if you do Undefined('function','or','variable')? In previous versions these commands are equivalent. It doesn't tell you that the character input is invalid? It makes sense, but it's definitely new behavior. What about a=2;b={};a(b). Or just a({1}) without initializing anything first? \$\endgroup\$ Jun 13 '17 at 9:26
  • \$\begingroup\$ It does, hence "function or variable". However, I think the old error message was misleading, because 'Undefined' does not have to be a function, but could indeed be a variable, and many (most) functions are defined for arbitrary input, seeing as there is no native way that I know of to define a function like y=f(uint32 x), so saying that a function is not defined for 'char' does not make sense. \$\endgroup\$
    – Sanchises
    Jun 13 '17 at 9:36
  • \$\begingroup\$ As for your examples: Undefined('function','or','variable') returns Undefined function or variable 'Undefined'. (indeed, you could be indexing into the variable Undefined); the latter two examples give Function 'subsindex' is not defined for values of class 'cell'. \$\endgroup\$
    – Sanchises
    Jun 13 '17 at 9:38
  • \$\begingroup\$ I agree, the new behavior makes more sense than the old one... I'm just surprised that they changed it. It makes this slightly outdated. \$\endgroup\$ Jun 13 '17 at 10:01
2
\$\begingroup\$

NASM for x86: 45 bytes

e.asm:1: error: parser: instruction expected

Assemble (or attempt to assemble) with nasm e.asm

Just in case:

0:beau@beau-Latitude-E6330:[~/asm/q]$ nasm -v
NASM version 2.12.02 compiled on Feb  5 2017
0:beau@beau-Latitude-E6330:[~/asm/q]$ 
\$\endgroup\$
2
\$\begingroup\$

Turing Machine Code, 70 bytes

Syntax error on line 1: <current symbol> should be a single character!

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Husk, 39 bytes

Could not infer valid type for program

Try it online! The infamous error that everyone is familiar with. Interestingly, I had trouble finding any other error quines: Parse error (missing \): ¶ is nearly one in verbose mode, but it outputs Parse error (missing \): ): ¶¶.

\$\endgroup\$
1
  • \$\begingroup\$ Haha.. Use the error to generate the error. \$\endgroup\$
    – Razetime
    Oct 22 '20 at 6:36
2
\$\begingroup\$

Swift

This is a bit of a stretch. Bear with me.

The source MUST be encoded as UTF-16. Formatting the following as UTF-16:

swift.swift:1:1: error: input files must be encoded as UTF-8 instead of UTF-16
??^@s^@w^@i^@f^@t^@.^@s^@w^@i^@f^@t^@:^@1^@:^@1^@:^@ ^@e^@r^@r^@o^@r^@:^@^@i^@n^@p^@u^@t^@ ^@f^@i^@l^@e^@s^@ ^@m^@u^@s^@t^@ ^@b^@e^@\^@e^@n^@c^@o^@d^@e^@d^@ ^@a^@s^@ ^@U^@T^@F^@-^@8^@ ^@i^@n^@s^@t^@e^@a^@d^@ ^@o^@f^@ ^@U^@T^@F^@-^@1^@6^@

where ^@ is emacs-speak for an embedded NUL and the ? characters are genuine ASCII question marks. Outputting to console (cat doesn't work, being ignorant of UTF16):

$ iconv -f utf-16 swift.swift

swift.swift:1:1: error: input files must be encoded as UTF-8 instead of UTF-16
??swift.swift:1:1: error: input files must be encoded as UTF-8 instead of UTF-16

and compiling:

$ swiftc swift.swift

We get:

swift.swift:1:1: error: input files must be encoded as UTF-8 instead of UTF-16
??swift.swift:1:1: error: input files must be encoded as UTF-8 instead of UTF-16

As desired.

CAVEATS:

  • The terminal is taking the 16-bit header BOM \xFF\xFE in the error message from swiftc and degrading it as ASCII ?? in the console.
  • When the swift compiler is run in an Emacs shell, the terminal settings do not process the unprintable ASCII characters, instead rendering them in Octal.
  • When the source is rendered in an Emacs buffer, which is UTF-16 aware, the first line appears correctly, and the ?? appears correctly, and but the remaining characters are shown interspersed with NUL literals rendered as ^@.
  • cat in the Terminal doesn't work either, being unaware of UTF-16.
  • A UTF16-aware output utility is needed: iconv -f utf-16 swift.swift, where the terminal emulator subsequently discards the interspersed NUL characters on the second line.
  • In MacOS, swiftc leverages terminal capabilities to produce bold and color escape codes. To avoid this ambiguity, you can use export TERM=dumb.

I'm claiming it's a quine as long as the source is rendered with a UTF-16 aware output utility and a terminal that discards ANSI formatting, NULs, and degrades unprintable characters to ?.

USING TextEdit

You can also make a more visually convincing argument using ˛ˇ in place of ?? where you keep the input as UTF-16 and let Swift output the (invalid) UTF-8 output with swiftc swift.swift > out 2>&1 and open out in TextEdit. Side by side, the input and output indeed are rendered identically. No less dubious under the covers, but looks a lot less dubious.

So...when you say exactly identical, what exactly do you mean?

If the output must be re-usable as the input, the quine constraint is indeed violated. And if you argue it's OK to cycle the UTF8 compiler output back to UTF16 source, that seems promising, but doesn't work because the output of swiftc is neither valid UTF-8 nor UTF-16: it is not valid to embed the BOM header midstream in either encoding. It's just terminal poo. Having fun yet?

It's an interesting foray into encoding and terminals, at the least!

\$\endgroup\$
2
\$\begingroup\$

Grok, 20 bytes

You don't grok Grok.

Try it Online!

Grok has only one error message, and one of the ways to trigger it is to try to execute an invalid command. In this case, oun'tgrG. are all invalid commands, so when the execution reaches the first o, it errors out, printing the source code.

\$\endgroup\$
2
\$\begingroup\$

Agda, 95 bytes

After a few attempts, I finally found a solution in my favorite dependently typed language! Agda includes the full filename in place of error.agda on lines 1 & 2.

error.agda:3,1-1
error.agda:3,1: Parse error
)<ERROR>
<ERROR>
<ERROR>
<ERROR>
<ERROR...

Save file as error.agda and prepend the full filepath on those first two lines for a local answer, or here: Try it online!

\$\endgroup\$
1
\$\begingroup\$

Cobra

test.cobra(1): error: Expecting use, assembly, namespace, class, interface or enum, but got "test".
Compilation failed - 1 error, 0 warnings
Not running due to errors above.
\$\endgroup\$
1
\$\begingroup\$

C++

(Using Apple LLVM in Xcode)

Unknown type name 'Unknown'
Expected ';' after top level declarator
\$\endgroup\$
1
\$\begingroup\$

ChucK

Here's my contribution:

[chuck]:line(1).char(8): syntax error

This works if you first type it into the editor, save it as "chuck", and then run it once. If you run it another time the number in char() goes up by eight.

ChucK can be downloaded here.

\$\endgroup\$
1
\$\begingroup\$

CJam

Syntax error:
java.lang.RuntimeException: y not handled
\$\endgroup\$
1
1
\$\begingroup\$

JavaScript

Uncaught SyntaxError: Unexpected identifier

Throws a generic Unexpected identifier error (in Chrome, at least) because it doesn't recognize Uncaught as an identifier.

\$\endgroup\$
2
  • \$\begingroup\$ I presume that the exact text this generates is not in the standard. Could you please include the browser / JavaScript engine that this submission is "programmed" in? \$\endgroup\$
    – wizzwizz4
    Oct 8 '16 at 12:57
  • \$\begingroup\$ It generates "SyntaxError: missing ; before statement" in Firefox. \$\endgroup\$
    – 12Me21
    Jan 25 '17 at 13:24
1
\$\begingroup\$

Codelike, 36 Bytes

Error at (1,1): Unknown character: E

Try it!

\$\endgroup\$
1
\$\begingroup\$

Ada

test.adb:1:01: compilation unit expected

Really just make an ada file, toss it at the start as above and compile with gcc filename.adb -c.

\$\endgroup\$
1
\$\begingroup\$

ForceLang

Works in the latest version of the reference implementation as of the time this answer was written.

Exception in thread "main" lang.exceptions.IllegalInvocationException: Exception is not a function.
    at lang.ForceLang.parse(ForceLang.java:52)
    at lang.ForceLang.main(ForceLang.java:129)
\$\endgroup\$
1
\$\begingroup\$

Python 3.5

Note: the source file should be named "1" for this to work

  File "1", line 1
    File "1", line 1
    ^
IndentationError: unexpected indent
\$\endgroup\$
1
\$\begingroup\$

GW-BASIC (least: 12 bytes)

Syntax error

Inspired by the Commodore 64 BASIC answer. Knocked off 2 bytes because IBM and DOS are better than Commodore :P You can download an emulator for GW-BASIC.

NEXT without FOR

Since GW-BASIC is a line-based language, it only evaluates the first instruction after a newline or colon :. Therefore, it reads NEXT and automatically fails it didn't read a FOR.

Apple ][ BASIC (13 bytes)

?SYNTAX ERROR

Woo, 1 less byte than the Commodore answer because the old Apples could beat a Commodore anyday :P

QBasic (43 bytes)

Parse failed: Syntax error at 1:1: Token(:)

Might be cheating because it only works on the first line.

Applesoft BASIC (69 bytes)

ParseError: Syntax error: Expected line number or separator in line 0

Also only works on the first line...

\$\endgroup\$
1
\$\begingroup\$

sed

By using this general approach, I have converged on several sed error quines. This may not work in your sed, in which case, you're using the wrong sed, and therefore would you please acquire necessary, error-quining sed.

Try it online! This version works in TIO, and probably not anywhere else. Open the "Debug" panel to see the error.

<code>sed: 1: test.sed: bad flag in substitute command: 'd'</code> and <code>gsed: file toast.sed line 1: extra characters after command</code>

Where sed is macOS's default sed, and gsed is a GNU sed.

\$\endgroup\$
1
\$\begingroup\$

Recursiva, 7 bytes

Error!

Try it online!

\$\endgroup\$
1
\$\begingroup\$

VB.Net, 32 Bytes

Create a new, empty project named 'A'. Set the startup object to "Sub Main" (it's a drop-down option). Create a new class called anything. Delete everything from the class and copy/paste the following:

'Sub Main' was not found in 'A'.

This works because a single quote is a comment, so it looks like a blank file :D

\$\endgroup\$
1
1
\$\begingroup\$

K (ngn/k), 2 bytes

'c

Try it online!

'c is the response from the console indicating that the variable c is undefined

\$\endgroup\$
1
  • \$\begingroup\$ Apologies, I've edited, thought what I was getting was STDERR as I was operating in the console and saw the output replicated by oK in TiO \$\endgroup\$
    – Thaufeki
    Oct 11 '18 at 17:29
1
\$\begingroup\$

MiLambda

ERROR_NOHALT

Try it online!

Seems like a short solution

\$\endgroup\$
1
\$\begingroup\$

Turing Machine But Way Worse, 258 bytes

Traceback (most recent call last):
  File "/opt/tmbww/TuringMachineButWayWorse.py", line 16, in <module>
    i[0], i[2], i[3], i[5], i[6] =  int(i[0]), int(i[2]), int(i[3]), int(i[5]), int(i[6])
ValueError: invalid literal for int() with base 10: 'Traceback'

Try it online!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ The rule about languages postdating the challenge has been abolished, you can compete all you want. \$\endgroup\$ Mar 22 '19 at 0:51
  • \$\begingroup\$ @ØrjanJohansen Oh, okay \$\endgroup\$
    – MilkyWay90
    Mar 22 '19 at 1:04
1
\$\begingroup\$

ZSH (macOS), 20 bytes excluding newline

 zsh: bad pattern: ^[
\$\endgroup\$
1
  • 1
    \$\begingroup\$ all error messages should include this: : ^[ \$\endgroup\$
    – roblogic
    Aug 23 '19 at 5:06
1
\$\begingroup\$

Ly

Error occurred at program index 3, instruction o (zero-indexed, includes comments)
EmptyStackError: cannot pop from an empty stack

Demo (using official compiler)

This program makes use of the o in "Error", the first instruction character in the error message that produces an error. (apparently r does nothing with an empty stack)

When a stack is not empty, o takes the top entry off the selected stack and outputs it as ASCII; EmptyStackError occurs when the selected stack is empty. So when Ly runs into this o, it causes this error, since we haven't given the stack anything yet.

\$\endgroup\$
1
\$\begingroup\$

W.Y.A.L.H.E.I.N., 253 bytes

Traceback (most recent call last):
  File "/opt/wyalhein/whenyouaccidentallylose100endorsementsinnationstates.py", line 6, in <module>
    seed = int(contents[0])
ValueError: invalid literal for int() with base 10: 'Traceback (most recent call last):\n'

Try it online!

whenyouaccidentallyloseahundredendorsementsinnationstates expects a seed on the first line, and Traceback (most recent call last): is not a valid number (unless we're talking in base 96, of course, but whenyouaccidentallyloseahundredendorsementsinnationstates doesn't).

\$\endgroup\$
2
  • \$\begingroup\$ You kinda get screwed by the length of the language name huh? \$\endgroup\$
    – Jo King
    Jun 18 '19 at 2:56
  • \$\begingroup\$ @JoKing Yeah. It was even shortened on TIO because the name was too long \$\endgroup\$
    – MilkyWay90
    Jun 18 '19 at 2:58
1
\$\begingroup\$

Loader (using the official Java interpreter):

These should be run from a module named main in order to produce the exact error messages given here.

This works in the most recent version of the interpreter:

Error: Could not evaluate expression Error (module main, line 1)

Explanation:

expression:statement is a conditional. The interpreter doesn't even syntax check the stuff to the right of the colon (if it did, we'd get a different error message) unless the stuff on the left evaluates to a nonzero value. However, as "Error" is an illegal expression, the interpreter can't evaluate it, exiting the program with this error message.

In some earlier interpreter versions, this would work instead:

Exception in thread "main" java.lang.RuntimeException: Could not evaluate expression Exception in thread "main" java.lang.RuntimeException (module main,line 1)
    at Loader.expr(Loader.java:183)
    at Loader.load(Loader.java:201)
    at Loader.main(Loader.java:249)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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