When to use try multi catch?

I do not understand when to use multi-trap. I saw several posts that compile the multi catch exception time type - this is the closest super type to several exception types.

Suppose that there are exception types A, B and their closest super type C.

option 1

try{//whatever} catch(A|B ex){//whatever} 

option 2

 try{//whatever} catch(C ex){//whatever} 

option 3

 try{//whatever} catch(A ex){//whatever} catch(B ex){//whatever} 

In what ideal cases should we use the above options when a few exceptions are thrown?

+4
source share
4 answers

According to Oracle documentation, the noteworthy points for the new block with several traps are:

 catch (IOException|SQLException ex) { logger.log(ex); throw ex; } 

If the catch block handles more than one type of exception, then the catch parameter is implicitly final . In this example, the catch ex parameter is final, and therefore you cannot assign any values ​​to it in the catch block. A bytecode generated by compiling a catch block that handles several types of exceptions will be less (and therefore superior) than compiling many catch blocks that handle only one type of exception each. The catch block, which handles several types of exceptions, does not duplicate the bytecode generated by the compiler; bytecode does not have exception handler replication.

If exceptions can be handled differently, I suggest that you should catch them separately. If exception handling is the same for multiple exceptions, you can use a block with multiple traps.

 try{//whatever} catch(A ex){//do something specific for A} catch(B ex){//do something specific for B} try{//whatever} catch(C ex){ //C is superclass of A and B and you are not concerned about the specific type // will catch even other exceptions which are instanceof C } try{//whatever} catch(A|B ex){//do the same thing for both A and B} 
+7
source

Option 1: Avoid duplicating code if A and B are handled the same way.

 } catch (SSLException | UnknownHostException e) { showErrorPopupAndReturn(); } 

Option 2: Almost the same as option 1, but it will also handle any other C subtype that you might not need.

 } catch (IOException e) { // Almost as Option 1, but will also handle any other subclass of // IOException, eg ObjectStreamException doStuff(); } 

Option 3: You need to do something else when A or B happens.

 } catch (UnknownHostException e) { tryAnotherIPaddress(); } catch (SSLException e) { reloadCertificate(); } 
+3
source

When to use each option:

option 1 : A or B do not have a common superclass, but the code for processing them will be the same

option 2 : A and B have C as a regular superclass, you just don't mind and treat them like the same

option 3 : A needs to be handled differently than B

0
source

It depends on how you handle the Exception.

In case 1, both types of Exceptions are caught and processed.

In case 2, he caught only type C and that is the exception of subtypes and handle

In case 3, both types A and B are processed separately.

the final output from both cases 1 and 3 are the same. but the comprehensibility or readability of the code will increase in case 3

0
source

Source: https://habr.com/ru/post/1488416/


All Articles