Catch question

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

slightlydead
Posts: 115
Joined: Wed Sep 23, 2009 10:52 pm UTC

Catch question

Postby slightlydead » Fri Feb 25, 2011 7:15 pm UTC

In general, the catch block is used because you want to locate where the error is found faster? I'm not quite seeing the benefits of it because the ide seems to detect where the error is w/o it.

User avatar
Robert'); DROP TABLE *;
Posts: 730
Joined: Mon Sep 08, 2008 6:46 pm UTC
Location: in ur fieldz

Re: Catch question

Postby Robert'); DROP TABLE *; » Fri Feb 25, 2011 7:20 pm UTC

Try running outside the IDE. :P

The catch block is used so that your program can recover from errors. For instance, the most common case is interacting with files. When the program tries to open a file and read from it, the OS might tell it "This file doesn't exist." At this point, the program will usually crash, unless a catch block has been used to catch the error and do something to recover from it. (Whether that's opening a different file, creating the file and then trying again, or simply saying "Oi!" and closing.)
...And that is how we know the Earth to be banana-shaped.

rflrob
Posts: 235
Joined: Wed Oct 31, 2007 6:45 pm UTC
Location: Berkeley, CA, USA, Terra, Sol
Contact:

Re: Catch question

Postby rflrob » Fri Feb 25, 2011 7:46 pm UTC

Try/catch follows the philosophy that it's better to beg forgiveness than ask permission. Using Bobby Table's file example, you could check before hand that the file exists, but that's only one of many things that could be wrong with the file. What if, while you're reading from the file, someone pulls out the hard drive? What if you're writing to the file and the disk is full? You could, in principle, check lots of these things before hand, but the important thing is that the operation fails, so rather than having lots of checks before hand, you can just say:

Code: Select all

try:
    write_to_file(data)
except IOError:
    print "There was some kind of error, you naughty child.
Ten is approximately infinity (It's very large)
Ten is approximately zero (It's very small)

slightlydead
Posts: 115
Joined: Wed Sep 23, 2009 10:52 pm UTC

Re: Catch question

Postby slightlydead » Fri Feb 25, 2011 8:51 pm UTC

oo thanks a lot guys, I'll try to drill this habit into myself then.

User avatar
Area Man
Posts: 256
Joined: Thu Dec 25, 2008 8:08 pm UTC
Location: Local

Re: Catch question

Postby Area Man » Sun Feb 27, 2011 6:30 pm UTC

Exception handling is for run-time errors, not syntax or compiler errors - OP seems a bit confused there.

What it does is let you separate the normal code flow from error routines, just as you divide your program into functions which take care of specific tasks.

This is apposed to intertwining the possible paths, and checking a global error number, special return value, or an extra parameter you pass in to functions, all over the place, as you would in plain old C.
Bisquick boxes are a dead medium.

Token
Posts: 1481
Joined: Fri Dec 01, 2006 5:07 pm UTC
Location: London

Re: Catch question

Postby Token » Sun Feb 27, 2011 6:40 pm UTC

Area Man wrote:Exception handling is for run-time errors, not syntax or compiler errors - OP seems a bit confused there.

I think the OP is referring to IDEs with debug modes and some kind of break-on-exception thing - which are fairly common.
All posts are works in progress. If I posted something within the last hour, chances are I'm still editing it.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 10 guests