public static void catchThrowable(Throwable e, Environment environment) {
        if (e instanceof BaseBaseException) {
            BaseBaseException baseException = (BaseBaseException) e;

            baseException.getEnvironment().catchUncaught(baseException);
            return;
        } else if (e instanceof Exception) {
            Environment env = environment == null ? null : environment;

            if (env != null) {
                try {
                    env.catchUncaught((Exception) e);
                } catch (RuntimeException e2) {
                    if (env.getDefaultBuffer() == null || env.getDefaultBuffer().getOutput() == null) {
                        e2.printStackTrace();
                    } else {
                        e2.getCause().printStackTrace(new PrintStream(env.getDefaultBuffer().getOutput()));
                    }
                }
                return;
            }
        }

        Environment env = environment == null ? null : environment;

        if (env != null) {
            e.printStackTrace(new PrintStream(env.getDefaultBuffer().getOutput()));
        } else {
            e.printStackTrace();
        }
    }
Gets Environment for current thread context
jphp-runtime/src/php/runtime/env/Environment.java