File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Capture excetion from singleton instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Capture excetion from singleton instance" Watch "Capture excetion from singleton instance" New topic
Author

Capture excetion from singleton instance

Abrahim Daver
Ranch Hand

Joined: Aug 06, 2004
Posts: 69
Hello there,
I am trying to create a singleton object. I am getting that object in the class and using it. Now, if there is an exception thrown while creating that singleton object, how can I handle that..
Please refer follwing code for better understanding of the issue-


If there is an exception thrown while getting instance of pReader, how can I handle that?

Abra D.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1770
    
    7

You could try and intialize the static variable from within a static initializer block and wrap the getInstance() call with a try-catch block, but why would you want to? If an Execption occurs at that level it seems to me that it would be pretty much fatal, so how would you propose to correctly handle it?


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Abrahim Daver
Ranch Hand

Joined: Aug 06, 2004
Posts: 69
Actually I am trying to access pReader from main method as well as from other methods of class abc. Hence I have declared pReader as static at class level. Is there a way to handle this kind of scenario?
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
You could do something like:
Abrahim Daver
Ranch Hand

Joined: Aug 06, 2004
Posts: 69
Thanks Lopes,That makes sense.
But what if my code is like this



I don't want to pass pReader object through startProcess method.
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Why are you making pReader static if it is already a singleton?


Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Capture excetion from singleton instance