tags:

views:

91

answers:

3
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...

http://stackoverflow.com/users/recent/454848

Please correct me if i am wrong...

  1. What does the setField method do? Do we need one.
  2. throw dbe would throw me the expection and the message being appended.
  3. What does chained construtor mean, is it like having multiple constructors.
+1  A: 

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.

Joe
+1  A: 
  1. 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.

  2. 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.

  3. 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.

casablanca
The chaining here takes advantage of the "special" chaining behavior of exceptions (the point I was making on John's earlier post) so that the underlying condition is not lost, and is displayed in the stack trace if this propagates out, and is available using the `getCause()` method or whatever...
andersoj
@andersoj: I see what you mean; that is more rightly termed "exception chaining", which is quite unrelated to "constructor chaining".
casablanca
+1  A: 

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

andersoj
The benefit of setField method being used, and will my throw dbe throw out this setField method value too..
John
@John: Not sure if I'm parsing your comment correctly, but I'll give it a go: The benefit of adding state to a custom exception -- i.e., adding a custom field and allowing you to set it using `setField()` -- is that you can propagate out state/error information to outer scopes that `catch` the exception. This obviates the need to explicitly pass out error or status via other means. And yes, if you call `setField(something)` on your `dbe` instance, then the code which catches the exception would be able to see it -- you might want to add a `DBGlobalsException.getField()` method for that.
andersoj