Skip to main content
Code Review

Return to Question

Post Locked by Mathieu Guindon
Notice added Historical significance by Mathieu Guindon
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

I ran a sonar analysis on my code and it told me the cyclomatic complexity is too high (sonar's limit is ten branches).

Here is my code:

public void myMethod(){
 try{
 // do something
 catch(SomeException e){
 throw new WrappingException("issue when doing something " + e.getMessage(), e);
 }
 catch(AnotherException e){
 throw new WrappingException("issue when doing something " + e.getMessage(), e);
 }
 // lots of other Exceptions
}

Basically, I want to catch a large set of Exceptions, (maybe always processing the same behaviour). I read the question Catching multiple types of exceptions when writing JSON Catching multiple types of exceptions when writing JSON but I don't use Java 7 (with which I could have all Exceptions in one catch statement) and I do not really want to catch ALL Exceptions, since I want my code to fail in a case I did not expect.

Is there any alternative that would involve fewer branches?

NOTE: What I want to achieve here is not to recover from any Exception, but to categorize the Exceptions. An upper layer is in charge of handling Exceptions, but for this purpose, Exceptions need to be properly categorized.

I ran a sonar analysis on my code and it told me the cyclomatic complexity is too high (sonar's limit is ten branches).

Here is my code:

public void myMethod(){
 try{
 // do something
 catch(SomeException e){
 throw new WrappingException("issue when doing something " + e.getMessage(), e);
 }
 catch(AnotherException e){
 throw new WrappingException("issue when doing something " + e.getMessage(), e);
 }
 // lots of other Exceptions
}

Basically, I want to catch a large set of Exceptions, (maybe always processing the same behaviour). I read the question Catching multiple types of exceptions when writing JSON but I don't use Java 7 (with which I could have all Exceptions in one catch statement) and I do not really want to catch ALL Exceptions, since I want my code to fail in a case I did not expect.

Is there any alternative that would involve fewer branches?

NOTE: What I want to achieve here is not to recover from any Exception, but to categorize the Exceptions. An upper layer is in charge of handling Exceptions, but for this purpose, Exceptions need to be properly categorized.

I ran a sonar analysis on my code and it told me the cyclomatic complexity is too high (sonar's limit is ten branches).

Here is my code:

public void myMethod(){
 try{
 // do something
 catch(SomeException e){
 throw new WrappingException("issue when doing something " + e.getMessage(), e);
 }
 catch(AnotherException e){
 throw new WrappingException("issue when doing something " + e.getMessage(), e);
 }
 // lots of other Exceptions
}

Basically, I want to catch a large set of Exceptions, (maybe always processing the same behaviour). I read the question Catching multiple types of exceptions when writing JSON but I don't use Java 7 (with which I could have all Exceptions in one catch statement) and I do not really want to catch ALL Exceptions, since I want my code to fail in a case I did not expect.

Is there any alternative that would involve fewer branches?

NOTE: What I want to achieve here is not to recover from any Exception, but to categorize the Exceptions. An upper layer is in charge of handling Exceptions, but for this purpose, Exceptions need to be properly categorized.

Tweeted twitter.com/#!/StackCodeReview/status/380349671220465664
added note on actual purpose
Source Link
Vince
  • 275
  • 3
  • 12

I ran a sonar analysis on my code and it told me the cyclomatic complexity is too high (sonar's limit is ten branches).

Here is my code:

public void myMethod(){
 try{
 // do something
 catch(SomeException e){
 throw new WrappingException("issue when doing something " + e.getMessage(), e);
 }
 catch(AnotherException e){
 throw new WrappingException("issue when doing something " + e.getMessage(), e);
 }
 // lots of other Exceptions
}

Basically, I want to catch a large set of Exceptions, (maybe always processing the same behaviour). I read the question Catching multiple types of exceptions when writing JSON but I don't use Java 7 (with which I could have all Exceptions in one catch statement) and I do not really want to catch ALL Exceptions, since I want my code to fail in a case I did not expect.

Is there any alternative that would involve fewer branches?

NOTE: What I want to achieve here is not to recover from any Exception, but to categorize the Exceptions. An upper layer is in charge of handling Exceptions, but for this purpose, Exceptions need to be properly categorized.

I ran a sonar analysis on my code and it told me the cyclomatic complexity is too high (sonar's limit is ten branches).

Here is my code:

public void myMethod(){
 try{
 // do something
 catch(SomeException e){
 throw new WrappingException("issue when doing something " + e.getMessage(), e);
 }
 catch(AnotherException e){
 throw new WrappingException("issue when doing something " + e.getMessage(), e);
 }
 // lots of other Exceptions
}

Basically, I want to catch a large set of Exceptions, (maybe always processing the same behaviour). I read the question Catching multiple types of exceptions when writing JSON but I don't use Java 7 (with which I could have all Exceptions in one catch statement) and I do not really want to catch ALL Exceptions, since I want my code to fail in a case I did not expect.

Is there any alternative that would involve fewer branches?

I ran a sonar analysis on my code and it told me the cyclomatic complexity is too high (sonar's limit is ten branches).

Here is my code:

public void myMethod(){
 try{
 // do something
 catch(SomeException e){
 throw new WrappingException("issue when doing something " + e.getMessage(), e);
 }
 catch(AnotherException e){
 throw new WrappingException("issue when doing something " + e.getMessage(), e);
 }
 // lots of other Exceptions
}

Basically, I want to catch a large set of Exceptions, (maybe always processing the same behaviour). I read the question Catching multiple types of exceptions when writing JSON but I don't use Java 7 (with which I could have all Exceptions in one catch statement) and I do not really want to catch ALL Exceptions, since I want my code to fail in a case I did not expect.

Is there any alternative that would involve fewer branches?

NOTE: What I want to achieve here is not to recover from any Exception, but to categorize the Exceptions. An upper layer is in charge of handling Exceptions, but for this purpose, Exceptions need to be properly categorized.

Source Link
Vince
  • 275
  • 3
  • 12

Reducing cyclomatic complexity

I ran a sonar analysis on my code and it told me the cyclomatic complexity is too high (sonar's limit is ten branches).

Here is my code:

public void myMethod(){
 try{
 // do something
 catch(SomeException e){
 throw new WrappingException("issue when doing something " + e.getMessage(), e);
 }
 catch(AnotherException e){
 throw new WrappingException("issue when doing something " + e.getMessage(), e);
 }
 // lots of other Exceptions
}

Basically, I want to catch a large set of Exceptions, (maybe always processing the same behaviour). I read the question Catching multiple types of exceptions when writing JSON but I don't use Java 7 (with which I could have all Exceptions in one catch statement) and I do not really want to catch ALL Exceptions, since I want my code to fail in a case I did not expect.

Is there any alternative that would involve fewer branches?

lang-java

AltStyle によって変換されたページ (->オリジナル) /