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

perl, 42 + 6 (file name) = 48 bytes

Illegal division by zero at /tmp/p line 1.

Try it online!

Save as contents of /tmp/p and run perl /tmp/p

Marking as community wiki because I did not come up with it, I saw it in this answer. I wanted to include it because I think it's beautiful.

explanation:

By default, text that doesn't parse as an expression in perl is treated as an unquoted string. This meants that at and tmp are just literal strings, and the / in between makes perl try to parse the division 'at'/'tmp'. Since both of them contain no numers, type coercion evaluates both of them to be 0, and obviously you can't divide by zero.

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

Fig, 1292 bytes (UTF-8)

DEBUG:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "x" is null
    at clojure.lang.Numbers.ops(Numbers.java:1068)
    at clojure.lang.Numbers.incP(Numbers.java:141)
    at fig.compression$fromBijectiveBase$fn__191.invoke(compression.clj:26)
    at clojure.core.protocols$fn__8181.invokeStatic(protocols.clj:168)
    at clojure.core.protocols$fn__8181.invoke(protocols.clj:124)
    at clojure.core.protocols$fn__8136$G__8131__8145.invoke(protocols.clj:19)
    at clojure.core.protocols$seq_reduce.invokeStatic(protocols.clj:31)
    at clojure.core.protocols$fn__8168.invokeStatic(protocols.clj:75)
    at clojure.core.protocols$fn__8168.invoke(protocols.clj:75)
    at clojure.core.protocols$fn__8110$G__8105__8123.invoke(protocols.clj:13)
    at clojure.core$reduce.invokeStatic(core.clj:6830)
    at fig.compression$fromBijectiveBase.invokeStatic(compression.clj:26)
    at fig.compression$decompress.invokeStatic(compression.clj:39)
    at fig.compression$decompress.invoke(compression.clj:35)
    at fig.compression$decompress.invokeStatic(compression.clj:36)
    at fig.parsing$lex.invokeStatic(parsing.clj:40)
    at fig.core$_main.invokeStatic(core.clj:38)
    at fig.core$_main.doInvoke(core.clj:19)
    at clojure.lang.RestFn.applyTo(RestFn.java:137)
    at fig.core.main(Unknown Source)

Try it online!

The tabs sadly disable Fig's scoring system, so this is counted in UTF-8/ASCII. Still no normal Fig quine...

\$\endgroup\$
1
  • \$\begingroup\$ not anymore :P \$\endgroup\$
    – naffetS
    Commented Oct 18, 2022 at 22:27
2
\$\begingroup\$

Desmos

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

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

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

MirBSD pax, 127 bytes.

Includes trailing newline:

pax: End of archive volume 1 reached
pax: Cannot identify format. Searching...
pax: Sorry, unable to determine archive format.

Usage

$ pax -f quine 
pax: End of archive volume 1 reached
pax: Cannot identify format. Searching...
pax: Sorry, unable to determine archive format.
$ pax < quine 
pax: End of archive volume 1 reached
pax: Cannot identify format. Searching...
pax: Sorry, unable to determine archive format.
$
\$\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\$

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