Exception Handlers

Exception handlers are only supported for BER decoding. BER is more amenable to error recovery than some other encoding rules.

To implement an exception handler, you do two things:

The following example is taken from the reader program of the java/sample_ber/ErrorHandlersample. In that sample, we illustrate ignoring all of the exceptions that can be ignored. You can see that some exceptions requuire skipping a TLV (tag-length-value), while others don't require any special action. The runtime documentation for Asn1BerExceptionHandler documents what is required to handle each type of exception. Exceptions that are not ignored are simply returned by the handler; if we had wanted to, the handler could have constructed some other exception and returned that, instead.

   public static class Handler implements Asn1BerExceptionHandler
   {
      public RuntimeException handleException(Asn1Exception e, 
                                       Asn1BerDecodeBuffer buffer)
      {
         try {
            if (e instanceof Asn1MissingRequiredException ||
               e instanceof Asn1InvalidEnumException) 
            {
               //ignore exception; no recovery action required
               System.out.println("IGNORED: " + e.getMessage());
               return null;
            }
            else if (e instanceof Asn1SeqOrderException ||
                     e instanceof Asn1UnexpectedElementException ||
                     e instanceof Asn1SetDuplicateException ||
                     e instanceof Asn1NotInSetException ||
                     e instanceof Asn1InvalidChoiceOptionException) 
            {
               //skip the offending element
               buffer.skipTLV();
               System.out.println("IGNORED: " + e.getMessage());
               return null;
            }
         }
         catch (java.io.IOException ignored) {
            //ignore: recovery failed
         }
         
         return e;
      }                                    
   }

Setting the exception handler is simple:

         // Create a decode buffer object
         Asn1BerDecodeBuffer decodeBuffer = 
            new Asn1BerDecodeBuffer (ins);
            
         decodeBuffer.setExceptionHandler(new Handler());