File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception handling: Throw not initialized - can someone explain this please?

 
Nigel Shrin
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sorry, I don't see what needs initializing, its a throw command with an instance of this simple Exception:
Is it illegal to nest another try within a finally block?







Thank you
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Local variables are not given their default value so you must initialize them. But because in your case this might not happen (only in the catch block) you must initialize it yourself.

Change
Whiz_MyException_ReThrow e1;
To
Whiz_MyException_ReThrow e1 = null;
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naj Shrin wrote:
Sorry, I don't see what needs initializing, its a throw command with an instance of this simple Exception:
Is it illegal to nest another try within a finally block?



For local variables, you are required to initialize the variable before you can use it. And the compiler will check every possible path -- if there is a path from declaration to usage without it ever being set (ie. initialized), the compiler will complain.

In this case, the compiler doesn't know that the f() method will always throw the exception, hence, the path is from the try block to the finally block -- with no exception being thrown.

Henry
 
Brij Garg
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from compiler point of view,
if function f() does not throw any exception then control will never reach to catch block and e1 will never be initialized. In that case in finally block e1 is used which is local to main method and not initialized. That is why compiler is complaining.

Henry has explained very well in the above post.


 
Nigel Shrin
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Wouter, Henry & Brij
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic