-
Enhancement
-
Resolution: Duplicate
-
P4
-
6
-
None
-
generic
-
generic
When you need to make an instance of ObjectName, you can use a constructor or you can use one of its static factory methods, but in all cases you must catch MalformedObjectNameException, which is a checked exception. This is a pain, especially when initializing fields. For example, you can't do this:
private static final ObjectName MY_NAME = new ObjectName("a:b=c");
Instead you have to do this:
private static final ObjectName MY_NAME;
static {
try {
MY_NAME = new ObjectName("a:b=c");
} catch (MalformedObjectNameException e) {
throw new IllegalArgumentException(e);
}
}
In practice, if you need to do this sort of thing a lot, you'll define a utility method that encapsulates the construction and the try/catch. Such a method (or set of overloaded methods) should be part of the API.
private static final ObjectName MY_NAME = new ObjectName("a:b=c");
Instead you have to do this:
private static final ObjectName MY_NAME;
static {
try {
MY_NAME = new ObjectName("a:b=c");
} catch (MalformedObjectNameException e) {
throw new IllegalArgumentException(e);
}
}
In practice, if you need to do this sort of thing a lot, you'll define a utility method that encapsulates the construction and the try/catch. Such a method (or set of overloaded methods) should be part of the API.
- duplicates
-
JDK-6734813 Provide a way to construct an ObjectName without checked exceptions
- Closed