More on exceptions

Discussion in 'Java' started by mike, May 29, 2008.

  1. mike

    mike Guest

    Hi,

    I am looking at the following code. If an UnsupportedEncodingException
    is thrown in getDD() how will that be handled by the code. Am I
    missing something? what is good practice?

    cheers,

    //mike


    public void myMethod {

    try {


    incomingDialog.sendStatus(dialog.getDD());
    } catch (IOException e) {
    logger.severe(e.getMessage());
    throw new ScenarioException(Reason.NETWORKERROR, e);
    }
    }


    The code above will call this code:

    protected Object getDD() {

    if (dd == null) {

    try {
    this.dd = invite.getContent();
    } catch (UnsupportedEncodingException e) {


    logger.severe(e.getMessage());
    throw new ScenarioException(Reason.MIMEERROR, e);
    } catch (IOException e) {

    logger.severe(e.getMessage());
    throw new ScenarioException(Reason.NETWORKERROR, e);
    }
    }

    return dd;
    }
    mike, May 29, 2008
    #1
    1. Advertising

  2. mike

    skyeweaver Guest

    On May 29, 10:38 am, mike <> wrote:
    > Hi,
    >
    > I am looking at the following code. If an UnsupportedEncodingException
    > is thrown in getDD() how will that be handled by the code. Am I
    > missing something? what is good practice?
    >
    > cheers,
    >
    > //mike
    >
    > public void myMethod {
    >
    > try {
    >
    > incomingDialog.sendStatus(dialog.getDD());
    > } catch (IOException e) {
    > logger.severe(e.getMessage());
    > throw new ScenarioException(Reason.NETWORKERROR, e);
    > }
    >
    > }
    >
    > The code above will call this code:
    >
    > protected Object getDD() {
    >
    > if (dd == null) {
    >
    > try {
    > this.dd = invite.getContent();
    > } catch (UnsupportedEncodingException e) {
    >
    > logger.severe(e.getMessage());
    > throw new ScenarioException(Reason.MIMEERROR, e);
    > } catch (IOException e) {
    >
    > logger.severe(e.getMessage());
    > throw new ScenarioException(Reason.NETWORKERROR, e);
    > }
    > }
    >
    > return dd;
    > }


    Hi Mike,
    inside the getDD() method there is a try-catch block which - if the
    UnsupportedEncodingException is throw from within it - will cause the
    exception to be caught by the catch(UnsupportedEncodingException e),
    then a log entry is made after which another exception is thrown, but
    this time in form of ScenarioException with some init parameter of
    "Reason.MIMEERROR" passed to its constructor.
    At this point this Exception will be thrown to the calling method
    which should either take care of it or throw it one level lower in the
    method stack hierarchy. However, what I can see
    is that the getDD method does not declare that it might throw
    ScenarioException which it should unless ScenarioException subclasses
    RuntimeException. If this is not the case it looks like the code will
    not compile.

    Regards
    skyeweaver, May 30, 2008
    #2
    1. Advertising

  3. mike

    skyeweaver Guest

    On May 30, 9:14 am, Lew <> wrote:
    > skyeweaver wrote:
    > > if the UnsupportedEncodingException is ...
    > > ... the calling method which should ...
    > > However, what I can see ... which it should unless ...
    > > If this is not the case it looks like the code will not compile.

    >
    > "If", "should unless" "looks like the code will not compile" - all the mystery
    > that could so easily be resolved, if the OP were only to heedhttp://sscce.org/
    >
    > --
    > Lew


    This is undoubtly a part of a whole source code cut off for the
    purpose of stating problem or arisen questions so there isn't much to
    test if it compiles because it does not. Not only because of problem I
    noticed but because it's not the a complete code anyway.
    Regards.
    skyeweaver, May 30, 2008
    #3
  4. mike

    skyeweaver Guest

    On May 30, 9:14 am, Lew <> wrote:
    > skyeweaver wrote:
    > > if the UnsupportedEncodingException is ...
    > > ... the calling method which should ...
    > > However, what I can see ... which it should unless ...
    > > If this is not the case it looks like the code will not compile.

    >
    > "If", "should unless" "looks like the code will not compile" - all the mystery
    > that could so easily be resolved, if the OP were only to heedhttp://sscce.org/
    >
    > --
    > Lew


    This is undoubtly a part of a whole source code cut off for the
    purpose of stating problem or arisen questions so there isn't much to
    test if it compiles because it does not. Not only because of problem I
    noticed but because it's not the a complete code anyway. Honestly, I
    think that what mattered here was not only to get to know what the
    outcome
    will be but a brief explanation what can happen if an exception is
    thrown.
    Regards.
    skyeweaver, May 30, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa
    Replies:
    5
    Views:
    29,992
    Chris Smith
    Jul 14, 2004
  2. Paul Miller
    Replies:
    3
    Views:
    1,009
    Alex Martelli
    Nov 12, 2003
  3. Michael
    Replies:
    4
    Views:
    399
    Matt Hammond
    Jun 26, 2006
  4. Replies:
    3
    Views:
    599
    Sherm Pendley
    Apr 16, 2007
  5. Robert Klemme

    With a Ruby Yell: more, more more!

    Robert Klemme, Sep 28, 2005, in forum: Ruby
    Replies:
    5
    Views:
    204
    Jeff Wood
    Sep 29, 2005
Loading...

Share This Page