4 ms·
Let me give you a real example from a program I worked on. I'll simplify the code. Say you write a function that takes input given by the user, inserts it int
by rkts 19y ago
Let me give you a real example from a program I worked on. I'll simplify the code.
Say you write a function that takes input given by the user, inserts it into a list, and then sends the action across the network. Every item has an ID, so when the function finishes, the current ID number needs to be incremented.
add_item (item) {
var item_id = current_id;
insert_item(item, item_id);
update_current_id();
send_item(item, item_id);
}
You write the function, and it works, and no one has any problems with it.
Two months later, you come back to the function and think, "That's messy. I should clean that up." So you do.
add_item (item) {
insert_item(item, current_id);
send_item(item, current_id);
update_current_id();
}
Do you see a problem with this code? No, you don't. You even run the program and try it out, and everything seems to work fine.
What if the function header had said "throws NetworkException"? You might have been more careful, but I wouldn't count on it. If the function were longer, which it would be in real life, you probably wouldn't catch your mistake.
On the other hand, what if the code looked like this?
add_item (item) {
if (insert_item(item, current_id) == INSERT_ERROR) {
return INSERT_ERROR;
}
if (send_item(item, current_id) == SEND_ERROR) {
return SEND_ERROR;
}
update_current_id();
return SUCCESS;
}
Do you see the problem now? Of course. It's staring you in the face: current_id won't get updated if the network goes down. The result is that you could have two items in your list with the same ID.
This isn't "nuclear power plant" paranoia. What I'm describing is a real bug I had recently. It wasn't the first time that I've done something like this, either.
I like your idea of putting the throws clause next to the function call. I'd like to see something like that for checked exceptions, and eliminate unchecked exceptions completely.
- olavk 19y agoOK, I think I get get it now. You (and Joel) are right. By reading your comment I realized that I have a number of coding habits that (in most cases) prevents this sort of error from occurring. But that proves your point that exception-based code is harder to get right. I still prefer to write exception-based code, but I understand how you could prefer to use return values.