Throwing an exception from your private constructor would be ok (reference This SO question, or do some quick Googling). In your case, you are catching the exception thrown from new UniApp()
and not passing it along - you can very easily pass that exception up the foodchain into your getInstance()
method and then to whomever calls that singleton.
For instance, using your code:
private MyTest() throws UniAppException { // Better if you declare _which_ exception UniApp throws!
// If you want your own code to log what happens, keep the try/catch but rethrow it
try{
uniApp = new UniApp("test","test123");
}
catch(UniAppException e) {
e.printStackTrace();
logger.error("Exception " +e+ "occured while creating instance of uniApp");
throw e;
}
}
public static MyTest getInstance() throws UniAppException {
if (test == null) {
synchronized(MyTest.class) {
if (test == null) {
test = new MyTest();
}
}
}
return test;
}
To create your "if" condition to test whether the getInstance()
method works or not, surround your call to getInstance()
with a try/catch block:
...
MyTest myTest;
try {
myTest = MyTest.getInstance();
// do stuff with an instantiated myTest
catch (UniAppException e) {
// do stuff to handle e when myTest will be null
}
...
Since you haven't shown what actually calls MyTest.getInstance()
I can't tell you what else to do besides that.