3 Programming
public Object[] myprimes(int nargout, Object n) throws MWException
{
(implementation omitted)
}
Any method that calls myprimes must do one of two things:
• Catch and handle the
MWException.
• Allow the calling program to catch it.
The following two sections prov ide ex amples of each.
Code Fragment: Handling an Exception in the Called Function
The getprimes example shown here uses the first of these methods. This
method handles the exception itself, and does not need to include a
throws
clause at the start.
public double[] getprimes(int n)
{
myclass cls = null;
Object[] y = null;
try
{
cls = new myclass();
y = cls.myprimes(1, new Double((double)n));
return (double[])((MWArray)y[0]).getData();
}
/* Catches the exception thrown by myprimes */
catch (MWException e)
{
System.out.println("Exception: " + e.toString());
return new double[0];
}
finally
{
MWArray.disposeArray(y);
3-20
Komentarze do niniejszej Instrukcji