More on exceptions  
Author Message
mike





PostPosted: 2008-5-29 16:39:00 Top

java-programmer, More on exceptions 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;
}
 
skyeweaver





PostPosted: 2008-5-30 15:05:00 Top

java-programmer >> More on exceptions On May 29, 10:38 am, mike <email***@***.com> 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
 
Lew





PostPosted: 2008-5-30 15:14:00 Top

java-programmer >> More on exceptions 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 heed http://sscce.org/

--
Lew
 
 
skyeweaver





PostPosted: 2008-5-30 15:49:00 Top

java-programmer >> More on exceptions On May 30, 9:14 am, Lew <email***@***.com> 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





PostPosted: 2008-5-30 16:14:00 Top

java-programmer >> More on exceptions On May 30, 9:14 am, Lew <email***@***.com> 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.