Can’t catch System.ProcedureException thrown by System.requestVersion()

I’m trying to write a simple method to extract the package version (using System.requestVersion()) and convert it to a string. The documentation for that method indicates that it will throw an exception if called from an unmanaged package. I’ve since found that it’s throwing a System.ProcedureException.

But I can’t figure out how to either:

  • Determine if I’m in a managed package before trying to call the method.
  • Catch the exception so I can return null to the caller.

The code below throws a System.ProcedureException: Method is not supported from an unmanaged namespace on the line calling System.requestVersion() but it isn’t caught by the catch block for some reason.

public static String packageVersion()
{
    try
    {
        System.Version v = System.requestVersion();
        String versionString = v.major() + '.' + v.minor();
        if (v.patch() != null)
        {
            versionString += '.' + v.patch();
        }
        return versionString;
    }
    catch (Exception e) { /* ignore exceptions and return null */ }

    return null;
}

I’ve tried both catching all exceptions (shown above) and just catching System.ProcedureExceptions. Both fail to catch.

Looking for help with either of the bullet points at the top of the question — code to know if it’s safe to call, or a way to catch the exception.

Answer

It looks like System.ProcedureException is yet another uncatchable exception. I think your only option here is to avoid having that exception thrown.

You can detect if you’re in an unmanaged context with some careful use of the UserInfo. isCurrentUserLicensed method. A careful read of the docs shows it throws a System.TypeException if the namespace passed is not installed in the current org, so you could make a method that detects an unmanaged context and avoids the call to requestVersion():

public static boolean isManaged(){
    boolean result;
    try{
        UserInfo.isCurrentUserLicensed('your_namespace');
        result = true;
    }catch(System.TypeException e){
        result = false;
    }
    return result;  
}

Since you only care if the package is managed, not if the user is actually licensed, you ignore the return value of isCurrentUserLicensed and only check if it throws a TypeException here.

Attribution
Source : Link , Question Author : tomlogic , Answer Author : ca_peterson

Leave a Comment