8 Replies Latest reply: Mar 5, 2007 9:43 AM by 807606 RSS

    Setter method in CustomException

    807606
      Is it advisable to have a setter method in my custom Exception class?
        • 1. Re: Setter method in CustomException
          807606
          to set what, exactly? I'd say "no"
          • 2. Re: Setter method in CustomException
            800435
            It depends of what your setter method does?Can you exemplify ?

            mleiria
            • 3. Re: Setter method in CustomException
              807606
              I can't think of any reason to have a setter on an exception. setters modify state after construction. if we raise an exception, and construct it with the correct state at the time it was raised, any subsequent change to that state means the exception might no longer describe what initially happened
              • 4. Re: Setter method in CustomException
                807606
                Let's say I have an object array as a memeber variable for my CustomException class. I also have constructor which will initialize the object array; on top of this I want to add some setter method where in I can set the desired object array.

                Can I have this setter? Is there any guidline to have setter method in an Exception class. I have never seen any setter method in the Exception class. Why is it so? Is there any reson behind this?

                Here is my sample program

                class CustomException extends Exception
                {
                     Object objArray[] = null;
                     Exception ex;
                     
                     public CustomException()
                     {
                     }
                     
                     public CustomException(Exception ex)
                     {
                          super(ex);
                          this.ex = ex;
                     }
                     
                     public CustomException(Exception ex, Object objArray[])
                     {
                          super(ex);
                          this.ex = ex;
                          this.objArray = objArray;
                     }
                     
                     public Object[] getObjectArray()
                     {
                          return objArray;
                     }
                     
                     // Can I have this setter? Is there any guidline to have setter method
                     // in an Exception class. I have never seen any setter method in the Exception class
                     // Why is it so? Is there any reson behind this?
                     public void setObjectArray(Object objArray[])
                     {
                          this.objArray = objArray;
                     }
                     
                }
                • 5. Re: Setter method in CustomException
                  807606
                  see above for why I think a setter has no place in an Exception. if you need to change the state of an exception after its creation, you've probably got design problems. what is this array for, exactly? why do you think you need a setter on your Exception?
                  • 6. Re: Setter method in CustomException
                    807606
                    The objArray is a placeHolder to hold the value objects involved while the exception had occured.
                    Initially the Exception object is constructed and thrown to the upper layer.
                    If at all I want to add some more objects/error codes for this exceptions I have some place to put. That is the main purpose of the array.
                    As you stated this is a kind of design issue. If it would have had a setter method to set the objectArray, then my problem is solved.

                    But my problem is I don't know whether an Exception class can have a setter method or not.

                    I have searched in Jave API. None of the Exception classes has the setter method. So only I got this doubt. I just would like to know the reason behind this.

                    If at all they did not have this setter methods just for a consistance, and if there will not be any harm to have a setter method then I can add one.

                    Thanks for your quick response.

                    Please clarify...
                    • 7. Re: Setter method in CustomException
                      807606
                      The objArray is a placeHolder to hold the value
                      objects involved while the exception had occured.
                      Initially the Exception object is constructed and
                      thrown to the upper layer.
                      If at all I want to add some more objects/error codes
                      for this exceptions I have some place to put. That is
                      the main purpose of the array.
                      As you stated this is a kind of design issue. If it
                      would have had a setter method to set the
                      objectArray, then my problem is solved.
                      why would you want to add more objects or error codes to it after the exception is raised? sounds like a recipe for disaster
                      But my problem is I don't know whether an Exception
                      class can have a setter method or not.
                      I have searched in Jave API. None of the Exception
                      classes has the setter method. So only I got this
                      doubt. I just would like to know the reason behind
                      this.
                      technically there is nothing to stop you doing it, no. the fact that nobody else does should give you a clue as to its appropriateness though :-)
                      If at all they did not have this setter methods just
                      for a consistance, and if there will not be any harm
                      to have a setter method then I can add one.
                      do it if you will, it's your code. I doubt there's many people would think it's a good idea, and I bet it ends up overcomplicating your code exponentially.
                      Thanks for your quick response.

                      Please clarify...
                      • 8. Re: Setter method in CustomException
                        807606
                        Thanks George for your quick response and your valuable inputs.

                        Cheers,
                        Vijay