Varargs in Exception

S

santax

In my work,InternalEx is used inner of my module,kinds of ExtenalEx is
used for other module calling this module.
the inter class is External, which covert InternalEx to ExtenalEx
responed.
as shown below, the code for covert Exception is reusable.
my improve class is also here.but it's so uglily,how can i use
Varargs feature here??

public class External {
public void m1() throws ExtenalEx1, ExtenalEx2, ExtenalEx3 {
Internal internal = new Internal();
try {
internal.m1();
} catch (InternalEx e) {
ExCode code = e.getCode();
switch (code) {
case ExtenalEx1:
throw new ExtenalEx1(e);
case ExtenalEx2:
throw new ExtenalEx2(e);
case ExtenalEx3:
throw new ExtenalEx3(e);
default:
break;
}
}
}
public void m2() throws ExtenalEx1, ExtenalEx2 {
Internal internal = new Internal();
try {
internal.m1();
} catch (InternalEx e) {
ExCode code = e.getCode();
switch (code) {
case ExtenalEx1:
throw new ExtenalEx1(e);
case ExtenalEx2:
throw new ExtenalEx2(e);
default:
break;
}
}
}
public void m3() throws ExtenalEx4, ExtenalEx5 {
Internal internal = new Internal();
try {
internal.m1();
} catch (InternalEx e) {
ExCode code = e.getCode();
switch (code) {
case ExtenalEx4:
throw new ExtenalEx4(e);
case ExtenalEx5:
throw new ExtenalEx5(e);
default:
break;
}
}
}
}

improved:
public class External {
public void m1() throws ExtenalEx1, ExtenalEx2, ExtenalEx3 {
Internal internal = new Internal();
try {
internal.m1();
} catch (InternalEx e) {
convert(e,ExtenalEx1.class,ExtenalEx2.class,ExtenalEx3.class);
}
}
public void m2() throws ExtenalEx1, ExtenalEx2 {
Internal internal = new Internal();
try {
internal.m1();
} catch (InternalEx e) {
convert(e,ExtenalEx1.class,ExtenalEx2.class);
}
}
public void m3() throws ExtenalEx4, ExtenalEx5 {
Internal internal = new Internal();
try {
internal.m1();
} catch (InternalEx e) {
convert(e,ExtenalEx4.class,ExtenalEx5.class);
}
}

private<T1 extends Exception,T2 extends Exception,T3 extends
Exception> void convert(InternalEx e,Class<T1> claz1,Class<T2>
claz2,Class<T3> claz3)throws T1,T2,T3{
convert(claz1,e);
convert(claz2,e);
convert(claz3,e);
}
private<T1 extends Exception,T2 extends Exception,T3 extends
Exception> void convert(InternalEx e,Class<T1> claz1,Class<T2>
claz2)throws T1,T2{
convert(claz1,e);
convert(claz2,e);
}
private static <T extends Exception> void convert(Class<T> claz1,
InternalEx e ) throws T{
ExCode code = e.getCode();
switch (code) {
case ExtenalEx1:
throw claz1.cast(new ExtenalEx1(e));
case ExtenalEx2:
throw claz1.cast(new ExtenalEx2(e));
case ExtenalEx3:
throw claz1.cast(new ExtenalEx3(e));
case ExtenalEx4:
throw claz1.cast(new ExtenalEx4(e));
case ExtenalEx5:
throw claz1.cast(new ExtenalEx5(e));
default:
break;
}
}
}
 
D

Daniel Pitts

In my work,InternalEx is used inner of my module,kinds of ExtenalEx is
used for other module calling this module.
the inter class is External, which covert InternalEx to ExtenalEx
responed.
as shown below, the code for covert Exception is reusable.
my improve class is also here.but it's so uglily,how can i use
Varargs feature here??
[snip code]

Its not about varargs, its about applying the right pattern.

Instead of throwing an internal exception, and then converting it to
the external exception, why not just throw the exception you mean in
the first place? That would seem to be the most sensible, and I can't
think of any reason not to. Perhaps if you gave a use-case where that
wouldn't work, I could suggest how to do it in a way that isn't uglily
[sic].
 
S

santax

used for other module calling this module.
the inter class is External, which covert InternalEx to ExtenalEx
responed.
as shown below, the code for covert Exception is reusable.
my improve class is also here.but it's so uglily,how can i use
Varargs feature here??

[snip code]

Its not about varargs, its about applying the right pattern.

Instead of throwing an internal exception, and then converting it to
the external exception, why not just throw the exception you mean in
the first place? That would seem to be the most sensible, and I can't
think of any reason not to. Perhaps if you gave a use-case where that
wouldn't work, I could suggest how to do it in a way that isn't uglily
[sic].

Sorry,my present is not clearly.
"higher layers should catch lower-level exceptions and, in their
place, throw exceptions that are explainable in terms of the higher-
level abstraction" (Effective Java P134)
in my module, every layer has it's own Exceptions. When a higher
layer(External.class) call a lower layer(Internal.class),It catch the
lower-level exceptions and convert it to higher-level exceptions.
 
D

Daniel Pitts

[snip code]
Its not about varargs, its about applying the right pattern.
Instead of throwing an internal exception, and then converting it to
the external exception, why not just throw the exception you mean in
the first place? That would seem to be the most sensible, and I can't
think of any reason not to. Perhaps if you gave a use-case where that
wouldn't work, I could suggest how to do it in a way that isn't uglily
[sic].

Sorry,my present is not clearly.
"higher layers should catch lower-level exceptions and, in their
place, throw exceptions that are explainable in terms of the higher-
level abstraction" (Effective Java P134)
in my module, every layer has it's own Exceptions. When a higher
layer(External.class) call a lower layer(Internal.class),It catch the
lower-level exceptions and convert it to higher-level exceptions.

Right, but it shouldn't do so in a generic manor.
If it could be done in a generic manor, than it is a higher level
exception to begin with.

Its perfectly reasonable to do something like:

public void doFoo() throws FooException {
try {
canThrowIOException();
canThrowBobException();
} catch (IOException e) {
throw new FooException("IO exception while fooing", e);
} catch (BobException e) {
throw new FooException("Bob didn't like to foo!", e);
}
}

It doesn't make sense to have IOException know about FooException, so
why would your IntException classes know about (even by ExCode)
ExExceptions?
 
S

santax

used for other module calling this module.
the inter class is External, which covert InternalEx to ExtenalEx
responed.
as shown below, the code for covert Exception is reusable.
my improve class is also here.but it's so uglily,how can i use
Varargs feature here??
[snip code]
Its not about varargs, its about applying the right pattern.
Instead of throwing an internal exception, and then converting it to
the external exception, why not just throw the exception you mean in
the first place? That would seem to be the most sensible, and I can't
think of any reason not to. Perhaps if you gave a use-case where that
wouldn't work, I could suggest how to do it in a way that isn't uglily
[sic].
Sorry,my present is not clearly.
"higher layers should catch lower-level exceptions and, in their
place, throw exceptions that are explainable in terms of the higher-
level abstraction" (Effective Java P134)
in my module, every layer has it's own Exceptions. When a higher
layer(External.class) call a lower layer(Internal.class),It catch the
lower-level exceptions and convert it to higher-level exceptions.

Right, but it shouldn't do so in a generic manor.
If it could be done in a generic manor, than it is a higher level
exception to begin with.

Its perfectly reasonable to do something like:

public void doFoo() throws FooException {
try {
canThrowIOException();
canThrowBobException();
} catch (IOException e) {
throw new FooException("IO exception while fooing", e);
} catch (BobException e) {
throw new FooException("Bob didn't like to foo!", e);
}

}

It doesn't make sense to have IOException know about FooException, so
why would your IntException classes know about (even by ExCode)
ExExceptions?

Yes,I am quite agree with your opinion.
but.. I am still in puzzle :(
Thanks.
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top