PDA

View Full Version : Java: make excpetion that throws multiple exceptions at one (ChargenServerExcpetion)



Eremis
October 9th, 2010, 10:13 PM
Hi everybody,

I have an assignment where I need to create an Exception which extends regular java.lang.Exception, which catches all ChargenServer related exceptions in one... (For ex. socket related exceptions, etc...)

This is all a part of a "big project" in which I need to create both server and client java applications for Chargen. (Both UDP and TCP implementations).

For some background... I am a freshman CS and I am in Networking, because i tested out of Java, so I never really "covered" everything in java... (Self-tough myself java) I understand how to use exceptions, but I just need help writing this one.

This is the description what this exception needs to have...:


The exceptions generated by the various concrete implementations of the ChargenServer interface’s listen()
method vary across implementations. For example, a TCP implementation might throw exceptions related
to TCP socket errors; a UDP implementation might throw other exceptions. We do not want users of
our interface to have to explicitly catch all these exceptions. (Remember, an interface presents an abstract
view of the component.) If a user is using only the UDP implementation, why make them catch TCP-
related exceptions? Instead, to provide an abstract interface, we introduce ChargenServerException. Classes
that provide concrete implementations of listen() will catch the protocol-specific exceptions, wrap those
exceptions in a ChargenServerException, and then throw that exception. This approach passes along all the
error information in the wrapped object, but keeps the interface “clean”.



Can someone help me, by maybie giving an example such as making mathOperationsExcpetion which catches multiple "math operations exceptions..."

or simply show me how to do this one...

Thanks

PS

I attached a UML diagram picture of the entire "server" side of this project...

Some Penguin
October 10th, 2010, 01:05 AM
http://download.oracle.com/javase/6/docs/api/java/lang/Exception.html

See how the standard Exception class offers a constructor which accepts another Throwable, which can then be retrieved using getCause()? Imitate that.

Eremis
October 10th, 2010, 01:14 AM
Can you give me an example?

Some Penguin
October 10th, 2010, 02:48 AM
Blargh. Well, here's one example of how exception wrapping works:



public class BlargException extends Exception {
public BlargException(Throwable cause) {
super(cause);
}

public BlargException(String message, Throwable cause) {
super(message, cause);
}
}




import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class WrappingDemo {
....
public static void throwsBlargException() throws BlargException {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(Integer.parseInt(br.readLine()) ;
} catch (NumberFormatException nfe) {
throw new BlargException(nfe);
} catch (IOException ie) {
throw new BlargException(ie);
} finally {
if (null != br) {
try {
br.close();
} catch (IOException ie) {
throw new BlargException(ie);
}
}
}
}
}



Then calling code would grab the wrapping exception and could identify the underlying one via getCause, if it cared to -- i.e.



try {
...
} catch (BlargException be) {
Throwable actualCause = be.getCause();
if (actualCause instanceof IOException) {
...
}
}

etc

or so forth, along those lines. What it buys you is not needing to say "int doesSomething() throws A, B, C, D, E" etc where A-E are all exception types.

Eremis
October 10th, 2010, 05:14 AM
ok, thx thats exactly what I was looking for