Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Capture excetion from singleton instance

 
Abrahim Daver
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Abrahim Daver
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do something like:
 
Abrahim Daver
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you making pReader static if it is already a singleton?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic