Catch

(Programming::C++ term)

See Also: try, throw, exception

Note: Please read try before reading this!

In the C++ programming language, the catch keyword can be used to encase a code block to handle various exceptions thrown by a try block. If you are not already familiar with the concepts of a try block, I encourage you to refer to that node first before continuing here. With that settled, let us evaluate the catch block in better detail:

Once a try block throws an exception, the program's execution unwinds, or "back-steps", through the code until it finds an appropriate catch block to handle that exception. If no catch statement/block is provided, the program defaults to whatever your compiler uses as its primary exception handler. More often than not, compilers allow the operating system to deal with the exception. This can result in (multiple) errors and other dangerous side effects (i.e. memory leaks).

Multiple catch blocks can be supplied for a single try block. This means that you are allowed to make multple catch blocks to handle multiple exceptions that may be thrown from one try block. A simple example of this is demonstrated using a string input system. This system checks for multiple conditions in which to throw an exception:

    #include <iostream>
    #include <string>

    using std::cout;
    using std::cin;
    using std::string;

    // Empty class to throw if string is empty
    class XNullString{ };

    // Emtpy class to throw if string is too long ( > 12 char's)
    class XStringTooLong{ };


    int main(int argc, char **argv){
        // We will use this object for our input
        string strInput;

        try {
            // Prompt the user for a string
            cout << "\nPlease input a word that is <= 12 characters long\n";
            cout << "->";
            cin >> strInput;

            // Test for empty string (note: most compilers won't accept a null string anyway)
            if ( strInput.empty() )
                // String is empty, return exception
                throw ( XNullString() );

            // Test for string length
            if ( strInput.size() > 12)
                // String is too long, throw exception
                throw ( XStringTooLong() );
            }

        // Catch the empty string exception
        catch ( XNullString ) {
            cout << "You didn't type anything in!\n";
            // Exit with error status ( return(1) )
            return(1);
            }

        // Catch the string length exception        
        catch ( XStringTooLong ) {
            cout << "The string is too long!\n";
            // Exit with error status ( return(1) )
            return(1);
            }

        // Nothing was thrown! Good!        
        cout << "You typed in: " << strInput << std::endl;
        // Return with successful status ( return(0) )    
        return(0);
        }


You are allowed to create a "catch everything" catch block. Inside of the parenthesis, instead of specifying an object, class, or other exception item, you can supply three ellipses (...). To demonstrate this technique, let's use the same code from before, but create only a single catch block:

    #include <iostream>
    #include <string>

    using std::cout;
    using std::cin;
    using std::string;

    // Empty class to throw if string is empty
    class XNullString{ };

    // Emtpy class to throw is string is too long ( > 12 char's)
    class XStringTooLong{ };


    int main(int argc, char **argv){
        // We will use this object for our input
        string strInput;

        try {
            // Prompt the user for a string
            cout << "\nPlease input a word that is <= 12 characters long\n";
            cout << "->";
            cin >> strInput;

            // Test for empty string (note: most compilers won't accept a null string anyway)
            if ( strInput.empty() )
                // String is empty, return exception
                throw ( XNullString() );

            // Test for string length
            if ( strInput.size() > 12)
                // String is too long, throw exception
                throw ( XStringTooLong() );
            }

        // Catch EVERYTHING
        catch (...) {
            cout << "Error Caught!\n";
            // Return with error status ( return(1) )
            return(1);
            }

        // Nothing was thrown! Good!        
        cout << "You typed in: " << strInput << std::endl;
        // Return with successful status ( return(0) )    
        return(0);
        }


If the try block in this program throws an exception, regardless of which exception it is, the single catch block will be executed. This is generally NOT a good habit to become accustomed to. If you know beforehand what possible exceptions may be thrown in your program, you should take the extra time to create the appropriate catch blocks to facilitate them. The "catch everything" block can always be used after all 'specific' catch blocks have been written, allowing the program to catch those specific exceptions...and if any unknown (or unexpected) exception occurs, catch it with the "catch everything" block.

Do NOT attempt to do anything in a catch block that resulted in that exception being thrown (ex. If you caught a failed allocation variable, do not try to allocate anything). This is emphasized in many C++ programming books, but often over-looked!.

Just as you can nest try blocks (within other try blocks), you can nest catch blocks within other try/catch blocks.


Please view the node throw for more detailed information on what you can throw!

Catch

My father came home with a new glove,
all tight stitches and unscuffed gold,
its deep pocket exhaling baseball,
signed by Mays or Mantle or “The Man,”
or some lesser god I’ve since forgotten.
He took off his tie and dark jacket
and we went outside to break it in,
throwing the ball back and forth
in the dusk, the big man sweating
already, grunting as he tried
to fire it at his son, who saw now,
for the first time, that his father,
who loved to talk baseball at dinner
and let him stay up late to watch the fights
unfold like grainy nightmares
on Gillette’s Cavalcade of Sports,
the massive father who could lift him
high in the air with one hand,
threw like a girl — far and away
the worst he could say of anyone —
his off-kilter wind-up and release
like a raw confession, so naked
and helpless in the failing light
that thirty years later, still
feeling the ball’s soft kiss in my glove,
I’m afraid to throw it back.

Copyright by George Bilgere



- - -



Evening Mr. Bilgere. Or...morning. The terms are debatable.

It's so difficult to not try too hard when writing to poets. I try to force every word into the shape of a sledgehammer and give it the appropriate heft with alliteration and whatnot and it just...it doesn't work. Plainspokenness is dreary and verbosity carries the possibility of being hackneyed and forced, so I'm caught. Fly-papered.

Damnit.


- - -


I came across "Catch" when I was in High School - it was published in an issue of American Scholar, I believe, and my father used to slip the quarterly into my bag (or, on one occasion, into my box of Lucky Charms) when I wasn't looking. It's been at the back of my head since then for a number of reasons that I couldn't quite place until last night. It's T.S. Eliot's fault; it usually is.

There were people in my apartment last night, along with a copy of the Norton Anthology of American Literature. I work in a used book store, a very, very large one, and am surrounded by words and people and word-people all night long. One of my guests was (is) a poet, and flipped the thing open to find Prufrock staring him in the face. He started reading, and off we went.

We started debating forms and words and meters and whatnot because, well, because there was beer involved and that's what happens when book store clerks take their work home with them. One of the people involved, it turned out, had an interesting disability, though intellectual or educational I'm not sure - she is incapable of hearing the difference between masculine and feminine syllables which, to be fair, makes it rather difficult to explain anything more poetically complicated. Because of this disability she has a hard time finding poems interesting for anything other than pure emotional response - she can't analyze and therefore doesn't see the point.

In a way, that's refreshing - it brings poetic plot to the forefront in a way that is usually hidden. We (at least, I) spend so much time searching for lyrical tricks and wit that meaning tends to become less important. In an attempt to help her, I went looking for works whose plot, like Prufrock's, is bare and yet whose metrical nature is still intrinsic to what we were looking at. And I remembered "Catch." And I Googled it - couldn't for the life of me remember the title, but searching for ("threw like a girl" poetry) worked wonders. In attempting to explain to this girl why poetry is poetry and prose is not, I realized that the way you tell stories on paper is the way I tell them to others - the stresses, the emphasis, the dip of intoned speech, all of it.

I'm not a disciple and, while I write poetry, I'm certainly no poet, caught somewhere between student and impostor with delusions of grandeur. But in some small way, "Catch" was an integral part of my lyrical upbringing.

Thank you, is what I'm saying. I don't know how often you hear that, but you've heard it from me.

Take care.

Jack Thompson



This Writeup conforms to current E2 Copyright policies. Just so you know.

Catch (?), v. t. [imp. & p. p. Caught (?) ∨ Catched (); p. pr. & vb. n. Catching. Catched is rarely used.] [OE. cacchen, OF. cachier, dialectic form of chacier to hunt, F. chasser, fr. (assumend) LL. captiare, for L. capture, V. intens. of capere to take, catch. See Capacious, and cf. Chase, Case a box.]

1.

To lay hold on; to seize, especially with the hand; to grasp (anything) in motion, with the effect of holding; as, to catch a ball.

2.

To seize after pursuing; to arrest; as, to catch a thief.

"They pursued . . . and caught him."

Judg. i. 6.

3.

To take captive, as in a snare or net, or on a hook; as, to catch a bird or fish.

4.

Hence: To insnare; to entangle. "To catch him in his words".

Mark xii. 13.

5.

To seize with the senses or the mind; to apprehend; as, to catch a melody.

"Fiery thoughts . . . whereof I catch the issue."

Tennyson.

6.

To communicate to; to fasten upon; as, the fire caught the adjoining building.

7.

To engage and attach; to please; to charm.

The soothing arts that catch the fair. Dryden.

8.

To get possession of; to attain.

Torment myself to catch the English throne. Shak.

9.

To take or receive; esp. to take by sympathy, contagion, infection, or exposure; as, to catch the spirit of an occasion; to catch the measles or smallpox; to catch cold; the house caught fire.

10.

To come upon unexpectedly or by surprise; to find; as, to catch one in the act of stealing.

11.

To reach in time; to come up with; as, to catch a train.

To catch fire, to become inflamed or ignited. -- to catch it to get a scolding or beating; to suffer punishment. [Colloq.] -- To catch one's eye, to interrupt captiously while speaking. [Colloq.] "You catch me up so very short." Dickens. -- To catch up, to snatch; to take up suddenly.

 

© Webster 1913.


Catch (?), v. i.

1.

To attain possession.

[Obs.]

Have is have, however men do catch. Shak.

2.

To be held or impeded by entanglement or a light obstruction; as, a kite catches in a tree; a door catches so as not to open.

3.

To take hold; as, the bolt does not catch.

4.

To spread by, or as by, infecting; to communicate.

Does the sedition catch from man to man? Addison.

To catch at, to attempt to seize; to be egger to get or use. "[To] catch at all opportunities of subverting the state." Addison. -- To catch up with, to come up with; to overtake.

 

© Webster 1913.


Catch, n.

1.

Act of seizing; a grasp.

Sir P. Sidney.

2.

That by which anything is caught or temporarily fastened; as, the catch of a gate.

3.

The posture of seizing; a state of preparation to lay hold of, or of watching he opportunity to seize; as, to lie on the catch.

[Archaic]

Addison.

The common and the canon law . . . lie at catch, and wait advantages one againt another. T. Fuller.

4.

That which is caught or taken; profit; gain; especially, the whole quantity caught or taken at one time; as, a good catch of fish.

Hector shall have a great catch if he knock out either of your brains. Shak.

5.

Something desirable to be caught, esp. a husband or wife in matrimony.

[Colloq.]

Marryat.

6. pl.

Passing opportunities seized; snatches.

It has been writ by catches with many intervals. Locke.

7.

A slight remembrance; a trace.

We retain a catch of those pretty stories. Glanvill.

8. Mus.

A humorous canon or round, so contrived that the singers catch up each other's words.

 

© Webster 1913.

Log in or register to write something here or to contact authors.