9

I have some code with this structure:

public void method() { Object o; try { o = new Object(); } catch (Exception e) { //Processing, several lines throw new Error(); //Our own unchecked exception } doSomething(o); } 

I have quite a few methods in which I have the same code in the catch block, so I want to extract it to a method so that I can save some lines. My problem is, that if I do that, I get a compiler error " The local variable o might not have been initialized".

public void method() { Object o; try { o = new Object(); } catch (Exception e) { handleError(); } //doSomething(o); compiler error } private void handleError() throws Error { //Processing, several lines throw new Error(); } 

Is there any workaround?

1
  • Note:Instance variable not need to initialize but the local variable(variable inside a method) need to initialize Commented Aug 7, 2013 at 9:25

6 Answers 6

8

You need to initialize local variables before they are used as below

public void method() { Object o=null; try { o = new Object(); } catch (Exception e) { handleError(); } doSomething(o); } 

You will not get the compilation failure until you use local variable which was not initialized

Sign up to request clarification or add additional context in comments.

2 Comments

Not really suitable if the variable has to be final for some reason. In this case I would favor the solution provided by @gyro-gearless.
This feels so gross. Is there no better style?
5

Since o is getting initialized within the try block and initializing o might throw an exception, java thinks that doSomething(o) statement might reach without o being initialized. So java wants o to be initialized incase new Object() throws exception.

So initializing o with null will fix the issue

public void method() { Object o = null; try { o = new Object(); //--> If new Object() throws exception then o remains uninitialized } catch (Exception e) { handleError(); } if(o != null) doSomething(o); } 

2 Comments

I'd consider this an anti-pattern, as it clutters the code with unnecessary if-not-null checks! The try/catch mechanism is there to avoid such pattern: if the creation of some required objects fails, the function should leave through an exception.
True, i agree. My intention was to give the solution and explain why we get error
4

Initialize your object: Object o = null;, however watch out for the NullPointerExceptions that might be thrown when you give it to the method calls.

Comments

3

Just put the doSomething(o) inside the try { } block:

public void method() { Object o; try { o = new Object(); doSomething(o); } catch (Exception e) { handleError(); } } 

You perhaps dont want to execute doSomething() if the creation of your Object fails!

Comments

0

Instance variable is the Object type so you should initialize value "null"

public void method() { Object o=null; try { o = new Object(); } catch (Exception e) { handleError(); } doSomething(o); } 

Comments

0

It happens because compiler doesn't know if handlerError always throws the Error. For example, there can be a logic in handleError method that makes it returns before throwing exception.

If you want to extract error handling into method, you can do this in this way:

public void method() { Object o; try { o = new Object(); } catch (Exception e) { throw handleError(); } System.out.println(o); } private Error handleError() { //Processing, several lines return new Error(); } 

Now the compiler sees the throw word in catch block, and it is sure that the exception will be thrown.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.