Manoj Kumar Giri
Manoj Kumar Giri

Reputation: 49

Exception Handling and initialization

I'm working on pre-build EJB application. I'm using Sonar for code improvements. I'm seeing this type of code block in almost every try-catch situation:

String ex = null;
try{
   ex = demo();
}catch(Exception e){
   ex=null;
   throw new Exception("message",e);
}

I want to ask that, writing "ex=null" in catch block is good or not? Means sonar is seeing it as useless code.

One more thing, the JDK version used to develop is JDK 6.

Thanks.

Upvotes: 2

Views: 62

Answers (3)

user6657880
user6657880

Reputation:

You don't need to do it. This is a bad exception handling approach.

Upvotes: 1

Nikem
Nikem

Reputation: 5784

Putting aside that this is very bad exception handling, no, you don't need to call ex=null.

Upvotes: 1

MartenCatcher
MartenCatcher

Reputation: 2887

You do not need to do it (ex=null). Variable ex will be uninitialized, when demo() throws exception.

Upvotes: 1

Related Questions