How to catch multiple Exceptions

C

Chris Berg

Which one is better/faster/smaller?:

(Yes, yes, "It depends on the VM", as always, but can anything more
clever be said about it?)

1)
public void myMethod() throws MyException{
try{
.. do something
} catch (MyException mye){
throw mye;
} catch Exception e){
.. deal with it
}
}

2)
public void myMethod() throws MyException{
try{
.. do something
} catch Exception e){
if (e instanceof MyException) throw (MyException)e;
.. deal with it
}
}

Chris
 
O

Oscar Kind

Chris Berg said:
Which one is better/faster/smaller?:

(Yes, yes, "It depends on the VM", as always, but can anything more
clever be said about it?)

1)
public void myMethod() throws MyException{
try{
.. do something
} catch (MyException mye){
throw mye;
} catch Exception e){
.. deal with it
}
}

2)
public void myMethod() throws MyException{
try{
.. do something
} catch Exception e){
if (e instanceof MyException) throw (MyException)e;
.. deal with it
}
}

Generally the first one. But not because it is smaller / faster (this is
irrelevant with exceptions as they normally never occur (they are
exceptional).

Generally the code handling the different exceptions is not very similar.
So if you create two code blocks to handle them, the code will be easier
to understand and maintain.

The second option is useful when there is only a small difference between
the exception handling code. Then you use that option to reduce
copy&paste (which is bad for debugging, maintenance and thus the
programmer's sanity).


Oscar
 
S

Sudsy

Chris said:
Which one is better/faster/smaller?:

(Yes, yes, "It depends on the VM", as always, but can anything more
clever be said about it?)

1)
public void myMethod() throws MyException{
try{
.. do something
} catch (MyException mye){
throw mye;
} catch Exception e){
.. deal with it
}
}

This is cleaner and more obvious to the reader. You can also insert
additional catch clauses as need be.
 
C

Chris Berg

Which one is better/faster/smaller?:

(Yes, yes, "It depends on the VM", as always, but can anything more
clever be said about it?)

1)
public void myMethod() throws MyException{
try{
.. do something
} catch (MyException mye){
throw mye;
} catch Exception e){
.. deal with it
}
}

2)
public void myMethod() throws MyException{
try{
.. do something
} catch Exception e){
if (e instanceof MyException) throw (MyException)e;
.. deal with it
}
}

Chris

Thanks.

I agree that 1) is cleaner looking, and would propably get higher
ranking at a university test. Nevertheless, in my particular case,
execution time is THE crucial issue, so I wonder if 2) executes
faster. It must depend on the way exceptions are actually detected and
handled at the interpreter level. ("depends on the VM.."). So, to
conclude something, 1) handles the special case in machine code, 2)
handles it in ByteCode, which is, of course, somewhat slower.

So unless someone comes up with a relly good reason to use 2), I think
I'll stick to 1).

Chris
 
M

Michael Borgwardt

Chris said:
I agree that 1) is cleaner looking, and would propably get higher
ranking at a university test.

code clarity is NOT an academic issue!
Nevertheless, in my particular case,
execution time is THE crucial issue, so I wonder if 2) executes
faster. It must depend on the way exceptions are actually detected and
handled at the interpreter level. ("depends on the VM.."). So, to
conclude something, 1) handles the special case in machine code, 2)
handles it in ByteCode, which is, of course, somewhat slower.

Don't presume, test! Performance decisions should not be based
on conjecture.
 
M

Michael Borgwardt

And another thing to think about: if performance is an issue at that point,
the best optimization you could do is to avoid the exception throwing
altogether, since the exception handling itself is quite slow (it's
*exceptional* after all!) and what exactly gets executed in the catch
blocks matters little in comparison. So exception-handling code shoul
*never* be executed frequently. And if it's not executed frequently,
performance doesn't matter.
 
T

Thomas Weidenfeller

Chris said:
I agree that 1) is cleaner looking, and would propably get higher
ranking at a university test. Nevertheless, in my particular case,
execution time is THE crucial issue, so I wonder if 2) executes
faster.

If performance is an issue, why do you use exceptions at all?

/Thomas
 
C

Chris Berg

And another thing to think about: if performance is an issue at that point,
the best optimization you could do is to avoid the exception throwing
altogether, since the exception handling itself is quite slow (it's
*exceptional* after all!) and what exactly gets executed in the catch
blocks matters little in comparison. So exception-handling code shoul
*never* be executed frequently. And if it's not executed frequently,
performance doesn't matter.

That's a good point.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

creating buffered image 4
Why catch specific exceptions 13
C++ newbie - trying to understand Exceptions 6
Error with server 3
exceptions 10
Exceptions 9
Where do unhandled exceptions go? 4
More on exceptions 3

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top