try and catch

  • Thread starter Thread starter Ernst Sauer
  • Start date Start date
E

Ernst Sauer

Hello,

it seems to me, that try and catch is (very?) expensive in time.
Is it?

Thanks
Ernst
 
it seems to me, that try and catch is (very?) expensive in time.
Is it?

Try catch without an exception should cost very little in
execution time.

Try catch with an exception cost some in execution time.

But as the name indicates then exceptions are intended for
situations that are exceptional. By definition something
exceptional should not be relevant for overall application
performance.

Let us take an example.

You reading 1 billion lines of text each with a number
and parsing them.

You decide to use exceptions for error handling.

If the file is good and no exceptions are throw, then
all is fine. No significant overhead.

If the file is garbage and do not contain numbers
then it depends on your code.

If your code catch the exception, display an error
message to the user and exit - then no performance
problem.

If your code catch the exception, increment a counter
and the continue to next line - when last is read and processed
notify the user of the number of errors and exit - then trowing and
catching 1 billion exceptions will be very significant.

But I would blame that on the code not on the
exception mechanism.

Arne
 
Back
Top