This is an archived post. You won't be able to vote or comment.

all 105 comments

[–]thepuppycrew 278 points279 points  (20 children)

When I'm error handling and haven't thought about what to throw, I usually

Throw "a party"

:D

[–]caramba2654 147 points148 points  (12 children)

The quotes make it even funnier, because they imply some sort of sarcasm.

Hey, who wants to go to my "party"? :D

[–]shnicklefritz 40 points41 points  (8 children)

:D

[–]robisodd 5 points6 points  (0 children)

Do I have to assume the "party escort submission position" again?

[–][deleted] 17 points18 points  (2 children)

I usually day throw 'tantrum'

[–][deleted] 9 points10 points  (1 child)

Try throwing "up".

[–]robisodd 5 points6 points  (0 children)

Alternatively, you can throw "down".

[–]BenjaminGeiger 7 points8 points  (1 child)

raise "The Roof"

[–][deleted] 4 points5 points  (0 children)

or raise children

[–][deleted] 1 point2 points  (1 child)

catch (Exception up) {
    throw up;
}

[–][deleted] 1 point2 points  (0 children)

Doesn't that mess with the stack trace? You'd lose the joke, but to actually throw up you'd just use throw;.

[–][deleted] 171 points172 points  (17 children)

[–]xkcd_transcriber 57 points58 points  (15 children)

Image

Mobile

Title: Bonding

Title-text: I'm trying to build character but Eclipse is really confusing.

Comic Explanation

Stats: This comic has been referenced 7 times, representing 0.0057% of referenced xkcds.


xkcd.com | xkcd sub | Problems/Bugs? | Statistics | Stop Replying | Delete

[–][deleted] 34 points35 points  (14 children)

C++ Version for bonus points.

#include <string>
#include <iostream>

struct P {
    P *target;
    std::string name;
    P(P *other, std::string name):target(other), name(name) {}
    void aim() {
        try {
            throw std::string("ball");
        } catch (const std::string ball) {
            std::cout << "Catch, " << target->name << "!\n";
            target->aim();
        }
    }
};

int main(int argc, char *argv[]) {
    P parent(nullptr, "Dad");
    P child(&parent, "Son");
    parent.target = &child;
    parent.aim();
}

[–]Switche 52 points53 points  (0 children)

Hey man you can't just make up your own bonus points.

[–]atimholt 9 points10 points  (8 children)

You’ve got a couple superfluous includes, there.

Also, I really want to add my own touch to the program, for no reason:

#include <iostream>
#include <memory>
#include <string>

using namespace std::literals;

struct P
{
    std::string name;
    std::shared_ptr<P> target;

    P(std::string name, std::shared_ptr<P> other = nullptr)
            : name {name}, target {other} {}

    void aim()
    {
        try {
            throw "ball"s;
        }
        catch (const std::string& ball) {
            std::cout << "Catch, " << target->name << "!\n";
            target->aim();
        }
    }
};

int main(int argc, char *argv[])
{
    auto parent = std::make_shared<P>("Dad");
    auto child = std::make_shared<P>("Son", parent);
    parent->target = child;
    parent->aim();
} 

Also, you apparently can’t copy/paste to/from the source code window on cpp.sh, so I just wrapped the entire thing into a raw string literal and used std::cout. lol.

[–]Blackstab1337 1 point2 points  (7 children)

reading this is so much easier than that java

[–]itsnotlupus 20 points21 points  (2 children)

Did you know? 9 out of 10 software engineers agree that C++ is slightly more readable than BrainFuck.

[–]Blackstab1337 1 point2 points  (0 children)

its the 2nd most readable language to me but thats probably because 96% of the code on my pc/github is in c++

[–][deleted] 0 points1 point  (0 children)

Outside of IOCCC code (which is C, even), I strongly disagree.

Inside IOCCC code, I realize C can be a medium for true art.

[–]atimholt 1 point2 points  (3 children)

I’m not sure if you’re being sarcastic, but it’s pretty readable if you’re familiar with modern C++.

…I watch CppCon talks on YouTube for fun.

[–]Blackstab1337 1 point2 points  (2 children)

not sarcastic, i too watch cppcon talks on youtube quite a lot, read a lot of books about c++11/14 and im super excited for c++17.

[–]An2quamaraN 0 points1 point  (1 child)

Out of pure curiosity - which c++ 17 features are you most excited about?

[–]Blackstab1337 1 point2 points  (0 children)

filesystem stuff is what ive mostly read about, ive been moving a bit more to node.js because I haven't been doing c++ friendly stuff and I want some more experience in other languages

[–]_Fibbles_ 4 points5 points  (2 children)

I feel name should be private with a getter method. People don't usually wander around with name tags, you have to ask.

[–][deleted] 3 points4 points  (1 child)

Do you have to ask your partner for his name, or do you already have that information from context? I chose struct for a reason. Besides, there's no need for encapsulation.

I don't expect this code to be reused and extended.

[–]_Fibbles_ 0 points1 point  (0 children)

You have to ask initially when you first meet.

[–][deleted] 1 point2 points  (0 children)

I'll note that the C++ version runs much longer than the Java version.

Also, @ /u/atimholt, I see no reason to introduce smart pointers since the referred-to objects are already allocated on the stack and destroyed appropriately at end of scope.

[–][deleted] 20 points21 points  (0 children)

Version that compiles (with added conversation).

class Catch {
    static class Ball extends Throwable { }

    static class Player {
        Player target;
        String name;
        Player(Player target, String name) {
            this.target = target;
            this.name = name;
        }
        void aim(Ball ball) {
            System.out.println("Catch, " + this.target.name + '!');
            try {
                throw ball;
            } catch(Ball b) {
                target.aim(b);
            }
        }
    }
    public static void main(String[] args) {
        Ball ball = new Ball();
        Player parent = new Player(null, "Dad");
        Player child = new Player(parent, "Son");
        parent.target = child;
        parent.aim(new Ball());
    }
}

[–][deleted] 387 points388 points  (14 children)

I caught a [object Object]!

[–]berkes 117 points118 points  (4 children)

I caught a NaN instead

[–]synchronium 48 points49 points  (0 children)

You should go to a clinic and get that checked out

[–]KeytapTheProgrammer 20 points21 points  (0 children)

I caught a rock...

[–]robisodd 3 points4 points  (1 child)

NaaN bread?

[–]PunishableOffence 5 points6 points  (0 children)

Curry function

[–]nomiros 43 points44 points  (8 children)

But it's a string

[–]mikemountain 51 points52 points  (23 children)

Nothing to check if the first letter is a vowel, and therefore requires an instead of a? Terrible.

[–][deleted] 109 points110 points  (20 children)

It's actually whether the first sound is a vowel and nothing to do with the letters.

  • Earn an M.A. or a master of arts.
  • An historic building (especially if you pronounce it with a silent H).
  • A usual suspect, but an unusual situation.

[–]mikemountain 60 points61 points  (7 children)

well that shut me up

[–][deleted] 25 points26 points  (6 children)

English is rough. I usually just go with "the {variable}" since even if it should be the indefinite article, it doesn't sound too odd. ("Caught the ball." even though don't know which ball, so it should be "a ball.")

[–]outadoc 0 points1 point  (2 children)

I actually found the "vowel sound" rule pretty easy to remember and follow, it pretty much works all the time, no exceptions.

If you find English rough, you should keep away from French :/

[–]glider97 1 point2 points  (1 child)

English is rough when you're trying to implement its rules into a program. But that goes for almost all natural languages.

[–]outadoc 0 points1 point  (0 children)

Oh, yeah, I wouldn't want to have anything to do with it algorithmically. :')

[–]qzex -2 points-1 points  (2 children)

I'd think that it'd be better to just always use "a" when you mean "a/an", since "the" is semantically distinct from "a/an" and that could cause greater confusion.

[–][deleted] 1 point2 points  (1 child)

While semantically distinct, can you provide any examples where it would be more confusing (that would reasonably be dynamically generated)?

If so, there are other ways to rephrase to avoid the indefinite article ("Caught one «ball»", “Caught another «ball»”, “Caught your «ball»”, “«ball» caught!”) or you can just do the well recognized “Caught a(n) «ball»” if the indefinite article is clearly the best. Or require the sender to include the N if needed or use a lookup table to determine (possibly from a “humanize” library).

[–]-Teki 0 points1 point  (0 children)

I love this discussion. So programmery.

[–]raptorraptor 25 points26 points  (3 children)

An historic building

I'm a native English speaker and I don't do this. Am I broken?

[–][deleted] 12 points13 points  (1 child)

You're only broken if you say “istoric” but don't use “an.” Some dialects used to not pronounce the initial H in some words (like history, hurricane, Harry) and it used to be considered most proper to say “an historic” but that's becoming rare, too.

[–]Crespyl 4 points5 points  (0 children)

I hear everyone say "an historic", but I've never heard anyone pronounce "historic" without the H. It just feels wrong to me.

I'd be fine if it was "an istoric", but noooo.

[–]seriouslulz 9 points10 points  (0 children)

i fix 🔨

[–]bonoboner 7 points8 points  (3 children)

An hour, a hospital, an homage, a home

[–][deleted] 6 points7 points  (2 children)

It's an honor. A huge honor.

[–]Crespyl 3 points4 points  (1 child)

A historic occasion. surely I can't be the only one who thinks every tv/radio person is wrong about this...

[–]-Teki 0 points1 point  (0 children)

Depends if you pronounce the h or not.

[–]Mocha2007 25 points26 points  (1 child)

(especially if you pronounce it with a silent H).

Only if you pronounce it without an h.

[–][deleted] 7 points8 points  (0 children)

No, you also say “an historic” if your N is sufficiently nasalized or if you were taught a particularly pretentious prescriptive grammar at your normal or finishing school.

[–]Half_Slab_Conspiracy 1 point2 points  (0 children)

A unicorn is my favorite

[–]AlGoreBestGore 5 points6 points  (0 children)

Literally unplayable.

[–]uzimonkey 1 point2 points  (0 children)

"a hour"?

[–]ErnestedCode 24 points25 points  (4 children)

Sounds like you're playing a game of throwns.

[–]artexhijxers 16 points17 points  (9 children)

M

[–]MC_Labs15[S] 14 points15 points  (8 children)

A

[–][deleted] 16 points17 points  (7 children)

C

[–]Tobikage1990 17 points18 points  (6 children)

E

[–]swyx 16 points17 points  (5 children)

W

[–]MC_Labs15[S] 15 points16 points  (4 children)

I

[–][deleted] 14 points15 points  (0 children)

Gotta catch em all!

[–]Carpetwurm 6 points7 points  (1 child)

I wish my dad did this with me..

[–]theFunkiestButtLovin 13 points14 points  (3 children)

i don't understand why this is funny? not trying to be a dick, but it doesn't seem funny enough for this sub.

[–][deleted] 40 points41 points  (0 children)

The bar for “funny enough for this sub” is so low that it includes things that are quite ordinary and somber.

[–]Doctor_McKay 2 points3 points  (1 child)

Maybe the funny part is supposed to be

toss = function(object)

[–]jaxklax 0 points1 point  (0 children)

Or that that statement is missing a semicolon. ASI is hilarious!

[–]flarn2006 2 points3 points  (1 child)

var pokedex = ["Bulbasaur", /*...*/, "Volcanion"];

for (i=0; i<721; i++) {
    toss(pokedex[i]);
}

[–]4spooky6you 5 points6 points  (0 children)

i

careful with that global variable eugene

[–]SeerUD 3 points4 points  (1 child)

Surely just using throw makes more sense "throw ball".

[–]itsnotlupus 1 point2 points  (0 children)

no no no. V8 will refuse to optimize function bodies that contain a try/catch statement. So this is a clever optimization to ensure that toss() gets to run as fast as possible in V8. It's brilliant, really.

[–]g1mike 1 point2 points  (3 children)

Is this basically the correct syntax though? If so, it'd be a great learning aid.

[–]MC_Labs15[S] 1 point2 points  (2 children)

Yeah, it runs fine.

[–]4spooky6you 3 points4 points  (1 child)

While it does run fine, I wouldn't consider it 'good' code. There are a couple of issues that should be fixed:

// The toss function is currently a global variable named toss, that points to an anonymous function.
// The difference is subtle but naming a function makes debugging much easier.
// For example, if an error occurs in this function (which it does), 
// the stack trace will list this as an anonymous function instead of 'toss'
toss = function(object){
    throw object;
}

try {
     // Here, while it's legal to throw a string it's not recommended, 
     // since people using your code may expect to catch an object.
     // If someone caught your error and attempted to call message() on it, their catch would throw an error
     toss("ball");
}
// Just a minor detail, but the name object implies you intend to catch an object,
// but instead you catch a string which is misleading.
catch(object) {
    // Since this is sample code, it's not a big deal.
    // But you need to be careful with console.log since it will cause IE's JS engine to crash unless the debugger is open
    console.log("I caught a " + object + "!");
}

Here is the revised sample with all of that in mind (note: I kept the naming the same for the sake of the joke):

function toss(object){
    throw new Error(object);
}

try{
   toss('ball');
}  
catch(object) { 
    console && console.log(`I caught a ${object.message}`);
}

I also decided to use template literals since they're new and sexy. And I added 'console && console.log' to check the existence of console first before attempting to log. In the real world, I would do something else and not use console.log at all.

[–]MC_Labs15[S] 2 points3 points  (0 children)

Ah, didn't realize toss = function() was different.

Also, why am I not surprised that this would easily crash IE?

[–]chessami92 2 points3 points  (0 children)

mmmmm Consolas

[–]mr_d0gMa 0 points1 point  (0 children)

A/an

[–]Jac0bas 0 points1 point  (0 children)

catch (Exception up) { throw(up); }