• Post Reply Bookmark Topic Watch Topic
  • New Topic

multy try catch efficiency  RSS feed

 
daniele licitra
Ranch Hand
Posts: 82
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all.

Supposing I've code with function throwing different exception, like that:


I want to use try-catch.
there are difference of efficiency between "multiple single try-catch" and "multycatch" ?
Example 1:


example 2:


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37465
538
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danielle,
catch(ExA, ExB, ExC e){ doesn't compile. It should be catch(ExA | ExB | Exc e) {

More importantly, your two code blocks don't do the same thing. If FuncThrowExA throws an exception, do you want FuncThrowExB to run? The answer to this dictates which choice you go with.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget aboiut speed and efficiency. The speed difference will only be a few nanoseconds. What you can do, to see how it works, is compile some methods with different try‑catch blocks, otherwise the same, and examine the bytecode with javap -c Foo
 
daniele licitra
Ranch Hand
Posts: 82
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply.

I'm sorry, i forgot the third case that is the real case:



I read some imap folder (fnew) and i save it on a DB.
Row (*) throw the messagin exception, row (**) launch some DB exception.
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the only thing i have heard about this is the rule that you catch the most specific exception first, then the more general exception. i would probably catch each separately.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!