39
2
Inspired by We had a unit test once which only failed on Sundays, write a program or function that does nothing but throw an error when it is Sunday, and exit gracefully on any other day.
Rules:
- No using input or showing output through the usual IO methods, except to print to STDERR or your language's equivalent. You are allowed to print to STDOUT if it's a by-product of your error.
- A function may return a value on non-Sundays as long as it doesn't print anything
- Your program may use a Sunday from any timezone, or the local timezone, as long as it is consistent.
- An error is a something that makes the program terminate abnormally, such as a divide by zero error or using an uninitialised variable. This means that if any code were to be added after the part that errors, it would not be executed on Sunday.
- You can also use statements that manually create an error, equivalent to Python’s
raise
. - This includes runtime errors, syntax errors and errors while compiling (good luck with that!)
- You can also use statements that manually create an error, equivalent to Python’s
- On an error there must be some sign that distinguishes it from having no error
- This is code-golf, so the shortest bytecount in each language wins!
I'll have to wait til Sunday to check the answers ;)
Sandbox Post – Jo King – 2018-01-05T12:21:04.237
Can the output be on STDOUT, as long it is generated by an error? – Rod – 2018-01-05T13:26:27.903
@Rod Yes you can – Jo King – 2018-01-05T13:28:47.597
2By "write a program or function that does nothing but throw an error on Sunday, and exit gracefully on any other day", do you mean that whenever it is run on sunday it should fail, or do you mean that there should be at least one possibility it fails a sunday. To make it clearer, if it fails only on sunday at 2pm, but not on sunday 3pm, is it fine ? – Bromind – 2018-01-05T13:53:16.167
@Bromind It has to fail at any time on Sunday – Jo King – 2018-01-05T14:03:54.050
Sad :'( I wanted to do something like
(time-time_of_first_sunday_after_epoch)%nb_of_second_per_week
– Bromind – 2018-01-05T14:08:29.623If we use a function instead of a full program, may the function return something (which we don't use whatsoever)? To perhaps explain it better: In Java I have to assign a value when I use divide, so instead of
()->{1/sundayValue;}
I'll have to use()->{int x=1/sundayValue;}
. This is a lambda with no parameter and no return-type. However, if I change this to a lambda with a return-type integer, I could do()->1/sundayValue
to shorten it. I don't use the output, and it still errors on division by 0, but I'm not sure if this is allowed? – Kevin Cruijssen – 2018-01-05T15:20:15.163@KevinCruijssen Sure. A function may return a value – Jo King – 2018-01-05T15:36:57.153
5This would have been even better if Saturday had been used. You could have called it "Saturday Night Error" and even worked in some adjusted song lyrics to the question. – Aaron – 2018-01-05T18:57:53.570
I was confused after seeing some of the answers to this. Does the program/function itself need to produce some output which you are considering an error, or does the logic to determine if something is output need to be triggered by some error in the code? I had assumed the former, but others seem to be assuming the latter. With my interpretation, Dennis' answer could be shortened by 2 characters by removing the back-ticks, otherwise they are necessary. – Aaron – 2018-01-05T19:09:31.163
3
Sundays? How about failing between midnight and 1am?
– Draco18s no longer trusts SE – 2018-01-05T20:25:38.6371It's not clear from your question what constitutes an error. I'm voting to close until this is remedied. – Post Rock Garf Hunter – 2018-01-06T16:28:05.960
@WheatWizard Better? – Jo King – 2018-01-06T18:18:17.573
3Not really ... How can we distinguish the output of an error from regular output. Is something like
print "error"
an error? The added paragraph doesn't really clarify anything. – Post Rock Garf Hunter – 2018-01-06T18:20:39.253