125
\$\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\$ Commented Aug 16, 2014 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\$ Commented Jun 1, 2015 at 12:57
  • 3
    \$\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\$ Commented Apr 10, 2017 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
    Commented May 7, 2017 at 21:50
  • 1
    \$\begingroup\$ @SteveBennett Why haven't you made that challenge yet? \$\endgroup\$ Commented May 28, 2017 at 1:53

134 Answers 134

3
\$\begingroup\$

A Pear Tree

a partridge

Try it online!

Almost every program (that isn't very long) prints a partridge in A Pear Tree.

One of the goals of the language was to be good at challenges; as such, it tries to find a substring of the program that has a very specific property that rarely occurs by chance, and starts running the program there. If it can't find an appropriate entry point, it prints an error. The actual text of the error message was chosen for the The Twelve Days of Christmas reference. (Actually, the design for the language worked the other way round; the main goal was to have a language which errored out on almost any source code, so that it would make the reference in question, so I set about trying to work out what sort of language design would naturally end up doing that.)

\$\endgroup\$
3
\$\begingroup\$

Setanta, 97 bytes

Eisceacht ar líne 1: Suíomh 19: Ag súil le uimhir, téacs, bool, athróg, liosta, nó gníomh.

Try it here!

\$\endgroup\$
3
\$\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\$
3
\$\begingroup\$

Desmos

Sorry, I don't understand the '"' symbol.

https://www.desmos.com/calculator/q0w3njcgxe

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Welcome to Code Golf! I seem to get a different error when I go to that link: You need something on both sides of the '.' symbol. \$\endgroup\$ Commented May 3, 2023 at 19:50
  • 1
    \$\begingroup\$ I couldn't make the code you posted work, but Sorry, I don't understand the '"' symbol. seems to do the trick. \$\endgroup\$ Commented May 3, 2023 at 20:50
  • \$\begingroup\$ Yes, I had tested it without the period. editing now. \$\endgroup\$ Commented May 4, 2023 at 0:36
  • \$\begingroup\$ Dupe answers are allowed per meta consensus, but keep in mind that this is essentially a dupe answer of this post. You also need to include the byte count in the header of your post. In your case that would be 41 bytes. \$\endgroup\$
    – Aiden Chow
    Commented May 13, 2023 at 8:37
3
\$\begingroup\$

Python 3, 105 bytes

  File ".code.tio", line 1
        File ".code.tio", line 1
        ^
IndentationError: unexpected indent

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\$

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
    Commented Jun 13, 2017 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\$ Commented Jun 13, 2017 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
    Commented Jun 13, 2017 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
    Commented Jun 13, 2017 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\$ Commented Jun 13, 2017 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\$
1
  • \$\begingroup\$ −3 B: The file name can be e. \$\endgroup\$ Commented Oct 27, 2022 at 21:49
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
    Commented Oct 22, 2020 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\$

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\$
2
\$\begingroup\$

TI-Basic, 24 bytes

ERR:SYNTAX
1:Quit
2:Goto

lowercase characters outside strings are a syntax error

screenshot of the error

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

Desmos, 41 bytes

Sorry, I don't understand the '?' symbol.

Try It On Desmos!

Hover over the danger sign to view the error message.

There could be something shorter but I don't have time to go through every single error out there in Desmos lol.

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

Desmos

Sorry, I don't understand the '&' symbol.

Go to desmos.com/calculator and paste in the "code".

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

Taxi

error: parse error: likely incomplete statement

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\$

Straw

/opt/straw/straw.rb:108:in `initialize': no implicit conversion of nil into String (TypeError)
    from /opt/straw/straw.rb:108:in `new'
    from /opt/straw/straw.rb:108:in `step'
    from /opt/straw/straw.rb:225:in `run'
    from /opt/straw/straw.rb:254:in `<main>'

Try It Online!

\$\endgroup\$
2
  • \$\begingroup\$ Works only if path to interpreter is /opt/straw/straw.rb. (Not that this answer is bad, just to let you know.) \$\endgroup\$
    – user48538
    Commented Sep 15, 2016 at 14:51
  • \$\begingroup\$ Oh, and also, Try it online! also outputs the timings for the program run and the exit code. \$\endgroup\$
    – user48538
    Commented Sep 15, 2016 at 14:51
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
    Commented Oct 8, 2016 at 12:57
  • \$\begingroup\$ It generates "SyntaxError: missing ; before statement" in Firefox. \$\endgroup\$
    – 12Me21
    Commented Jan 25, 2017 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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