Reputation: 1323
I'd like to instantiate an object with it's constructor outside a method. Example:
public class Toplevel {
Configuration config = new Configuration("testconfig.properties");
public void method1() {
config.getValue();
...etc
}
}
If I do this right now...I get this error..
Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit constructor
I'd like to do something like this so I could call config anywhere in my class, right now I keep having to instantiate the Configuration objects...there's gotta be a way to do this...any help would be much appreciated, thanks in advance.
EDIT:
Configuration class:
public class Configuration {
private String mainSchemaFile;
public Configuration() {
}
public Configuration( String configPath ) throws IOException {
Properties prop = new Properties();
prop.load( new FileInputStream( configPath ));
this.mainSchemaFile= prop.getProperty("MAINSCHEMA_FILE");
}
Upvotes: 0
Views: 899
Reputation: 178253
Your Configuration
constructor is declared to throw an IOException
. Any code that instantiates a Configuration
using this constructor must catch it. If you use a variable initializer, then you can't catch it, because you can't supply a catch
block; there is no block you can put here, only an expression. There is no method to declare a throws
clause on either.
Your alternatives:
Instantiate the Configuration
in a Toplevel
constructor. You can catch
the exception in the constructor body, or you can declare that constructor that it throws
the exception.
public class Toplevel {
Configuration config;
public Toplevel() {
try {
config = new Configuration("testconfig.properties");
} catch (IOException e) { // handle here }
}
// ...
Instantiate the Configuration
in an instance initializer in the TopLevel
class, where you can catch
the exception and handle it.
public class Toplevel {
Configuration config;
{
try {
config = new Configuration("testconfig.properties");
} catch (IOException e) { // handle here }
}
// ...
Catch and handle the exception in the Configuration
constructor, so calling code doesn't have to catch the exception. This isn't preferred, because you may have an invalid Configuration
object instantiated. Calling code would still need to determine if it's valid.
public class Configuration {
// Your instance variables
private boolean isValid;
public Configuration( String configPath ) {
try {
// Your code that might throw an IOE
isValid = true;
} catch (IOException e) {
isValid = false;
}
}
Upvotes: 2
Reputation: 58
When you create a new Toplevel object then you have not declared a specific constructor for it and the attribute of Toplevel is instantiated as your code describes it with Configuration config = new Configuration("testconfig.properties");
So you do not handle the IoException of the Configuration constructor! A better way would be to declare a specific constructor of Toplevel like this:
public Toplevel(){
try{
this.config = new Configuration("testconfig.properties");
} catch (IOException e) {
// handle Exception
}
}
Upvotes: 1