English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I've been doing a small bit of reading on try/catch statements and I am wondering what exactly the point is of them. From what I have read it seem thats you need to throw an exepction in order to catch it, meaning that you (the programmer) actually need to hard code that expection and make a catch statement for it. I do not really see what the advantage is over the case or if/else if/else statement.

2006-12-31 15:58:46 · 4 answers · asked by D 4 in Computers & Internet Programming & Design

4 answers

yes, you could do the same thing with if-else, of course, it'd just be more cumbersome.
Particularly, when you call a function, that might return an error condition, that you don't want to handle - just pass it up to the caller...
int status = thisFunction();
if (status==OK) {do_something();}
return status;

Now the caller function would have to do something similar too if it is not prepared to handle the possible error condition, etc...
The exception mechanism is intended to make these things easier - you only catch exceptions that you want to handle, and all the others go up the stack automagically, without you even having to know about them. Your function form the above example would look like just:
thisFunction();
doSomething();

if an error occurs in thisFunction(), it'll just throw an exception, anf doSomething() will never get executed - no need to explicitly check for the status, unless, you have a reasno to expect an error, and know how to handle it, in which case, you'd catch the exception.

2006-12-31 17:31:49 · answer #1 · answered by n0body 4 · 1 0

The actual term used for try-catch statements is Exceptional Handling.

Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of some condition that changes the normal flow of execution. The condition is called an exception. Alternative concepts are signal and event handler.

In general, current state will be saved in a predefined location and execution will switch to a predefined handler. Depending on the situation, the handler may later resume the execution at the original location, using the saved information to restore the original state. For example, an exception which will usually be resumed is a page fault, while a division by zero usually cannot be resolved transparently.

From the processing point of view, hardware interrupts are similar to resumable exceptions, except they are usually not related to the current program flow.

From the point of view of the author of a routine, raising an exception is a useful way to signal that the routine cannot execute normally, for example when its input arguments are invalid (a zero denominator in division) or when a resource it relies on is unavailable (a missing file, or a disk error). In systems without exceptions, the routine would need to return some special error code, but that causes the semipredicate problem, where users of the routine are forced to write their own code to tell normal return values from erroneous ones.

So you can judge, that

1)try-catch provides an easy way to exactly know the source of the error...

2)if a runtime error-occurs, even then the program can be made to run if you are using try-catch

3)they are particularly helpful in system programming or in lengthy code...

.

2006-12-31 17:01:06 · answer #2 · answered by Anonymous · 0 1

Functions and methods that you call can also throw exceptions, which you generally want to catch and deal with rationally.

In some languages (Python, particularly), it can be clearer to code to the general case and deal with exceptions as, well, the exception. This is referred to as "do and apologize later"--compare to the if/else's "ask first". If the exceptional condition really is much less common than the general case, this can be faster that "ask first," too.

Also, if you have an insane number of nested loops that you need to break out of (since you can't say "break; break; break;" to leave the innermost three loops, or because setting up a mess of flags is confusing and ugly), you can throw an exception and catch it. I've done something slightly similar in JavaScript, though for a somewhat different purpose: http://freecog.net/2006/asyncqueue/ Of course, this sort of flow control is generally poor style.

2006-12-31 16:14:08 · answer #3 · answered by Tom W.M. 1 · 0 0

If you are using java then the try catch statement will help with error protection. If an ioexception, like the file not being there occurs the program will catch itself and stop a crash by using the code in the catch statement to control the situation like ask for a new file name.

2006-12-31 16:09:00 · answer #4 · answered by Bucky 2 · 0 1

fedest.com, questions and answers