EXCEPTIONCLEAR Method

Clears any exception that is currently being thrown.

Category: Exception
Applies to: Java object

Syntax

object.EXCEPTIONCLEAR( );

Arguments

object

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 take appropriate action and then clear the exception by using the EXCEPTIONCLEAR method.
If no exception is currently being thrown, this method has no effect.

Examples

Example 1: Checking and Clearing an Exception

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

Example 2: Checking for an Exception When Reading an External File

In 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, which is the parent of FileInputStream, and the current method lookup is not robust enough to perform 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;