Java Object Language Elements |
Category: | Exception |
Syntax | |
Arguments | |
Details | |
Example | |
Checking and Clearing an Exception | |
Chekcing for an Exception When Reading an External File | |
See Also |
Syntax |
object.EXCEPTIONCLEAR( ); |
specifies the name of the java object.
Details |
Java exceptions are handled through the EXCEPTIONCHECK, EXCEPTIONCLEAR, and EXCEPTIONDESCRIBE methods.
If you call a method that throws an exception, it is strongly recommended that you check for an exception after the call. If an exception was thrown, you should perform some appropriate action and then clear the exception by using the EXCEPTIONCLEAR method.
If no exception is currently being thrown, this method has no effect.
Example |
In the following example, the Java class contains a method that throws an exception. The method is called in the DATA step and the exception is cleared.
/* Java code */ public class a { public void m() throws NullPointerException { throw new NullPointerException(); } }
/* DATA step code */ data _null_; length e 8; dcl javaobj j('a'); rc = j.callvoidmethod('m'); /* Check for exception. Value is returned in variable 'e' */ rc = j.exceptioncheck(e); if (e) then put 'exception'; else put 'no exception'; /* Clear the exception and check it again */ rc = j.exceptionclear( ); rc = j.exceptioncheck(e); if (e) then put 'exception'; else put 'no exception'; run;
The following lines are written to the SAS log.
exception no exception
Iin this example, the Java IO classes are used to read an external file from the DATA step. The Java code creates a wrapper class for DataInputStream which enables you to pass a FileInputStream to the constructor. The wrapper is necessary because the constructor actually takes an InputStream, the parent of FileInputStream, and the current method lookup is not robust enough to do the superclass lookup.
/* Java code */ public class myDataInputStream extends java.io.DataInputStream { myDataInputStream(java.io.FileInputStream fi) { super(fi); } }
After you create the wrapper class, you can use it to create a DataInputStream for an external file and read the file until the end-of-file is reached. The EXCEPTIONCHECK method is used to determine when the readInt method throws an EOFException, which enables you to end the input loop.
/* DATA step code */ data _null_; length d e 8; dcl javaobj f("java/io/File", "c:\temp\binint.txt"); dcl javaobj fi("java/io/FileInputStream", f); dcl javaobj di("myDataInputStream", fi); do while(1); di.callIntMethod("readInt", d); di.ExceptionCheck(e); if (e) then leave; else put d=; end; run;
See Also |
Method: |
Copyright © 2011 by SAS Institute Inc., Cary, NC, USA. All rights reserved.