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