More on exceptions

M

mike

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;
}
 
S

skyeweaver

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
 
S

skyeweaver

"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/

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

skyeweaver

"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/

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top