Reputation: 1189
final class DBGlobalsException extends Exception
{
String mistake;
//where this is shorthand for chained constructors that look like Exception
DBGlobalsException(String message, Throwable cause)
{
super(message,cause);
}
// elaborate your exception with whatever error state you want to propagate outwards
void setField(mistake) {
}
}
catch (IOException ex) {
DBGlobals.Error("OHMjson.Graph.saveLastGraphName - Error: " + ex.getMessage());
msg = "Unable to save data";
status = false;
DBGlobalsException dbe = new DBGlobalsException(msg,ex);
dbe.setField(status);
throw dbe;
}
This post code is taken from my previous post...
https://stackoverflow.com/users/recent/454848
Please correct me if i am wrong...
Upvotes: 1
Views: 1405
Reputation: 22874
1) What does the setField method do? Do we need one.
The setField()
method was a method I introduced in the previous question to show you how to add new state to an exception. Since I don't know the specifics of your application, I don't know what specific information needs to be propagated. Here, it may be enough to know that something went wrong under the covers, and you should just wrap the IOException
or whatever as the "cause" in your custom exception.
2) throw dbe would throw me the expection and the message being appended.
Yes, throw dbe
throws the new, chained exception. If you don't catch it, yes, the messages from the two would be appended in the resulting stack trace. As described in the Throwable
API, you can customize how the "message" is presented/composed.
3) What does chained construtor mean, is it like having multiple constructors.
As pointed out in another answer, the chained constructor just refers to constructors which call other constructors. In the case of subclasses of Throwable
, however, there are specific superclass constructors provided in order to chain exceptions, which give you a standard mechanism to "wrap" underlying exceptions in outer exceptions, more appropriate to the level of abstraction of your code. See the description in the Throwable
API
Upvotes: 1
Reputation: 70691
It seems you have only posted parts of the actual code, from which it is impossible to say exactly what setField
does. It possibly just sets the mistake
field, and assuming there is a similar getField
method, it can be retrieved elsewhere when it is required.
throw dbe;
does just what it says: it throws the dbe
object, which implicitly includes the message and any other fields declared in the class.
By constructor chaining, are you referring to the super(message,cause);
line? If so, that simply calls the superclass (Exception
) constructor. Essentially, the DBGlobalsException
constructor has nothing special to do, so it just forwards its parameters to the superclass.
Edit: From what @andersoj mentioned in a comment, it seems what he really meant in the previous answer was "exception chaining", which is unrelated to constructors in general. Exception chaining basically lets you link an exception to another one which caused it. In your example, the DBGlobalsException
is thrown in response to an IOException
, hence by passing ex
as the cause
argument when creating dbe
, you set up a link that says "ex
caused dbe
". When you later catch dbe
, you can use getCause()
to obtain the linked exception, which is ex
.
Upvotes: 1
Reputation: 3347
a chained constructor means you have one constructor that takes all "possible" parameters you might send and all other constructors simply call (chain) to this one. They usually provide default values for some parameters. All logic needed to be done in the main/chained constructor so code is not duplicated.
public class Movie {
private String title;
private String rating;
public Movie() {
this("G"); // calls-chains -- to main defualt constructor - note the default value passed
}
public Movie(String newRating) {
rating = newRating;
}
}
as for the error - if setfield takes the string value and sets the mistake string, then then yes it's needed. If the message is the same thing as the mistake string then no, you can set it via the constructor.
Upvotes: 1