org.apache.sis.util.logging.Logging.unexpectedException()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(12.2k)|赞(0)|评价(0)|浏览(123)

本文整理了Java中org.apache.sis.util.logging.Logging.unexpectedException()方法的一些代码示例,展示了Logging.unexpectedException()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Logging.unexpectedException()方法的具体详情如下:
包路径:org.apache.sis.util.logging.Logging
类名称:Logging
方法名:unexpectedException

Logging.unexpectedException介绍

[英]Invoked when an unexpected error occurred. This method logs a message at Level#WARNINGto the specified logger. The originating class name and method name can optionally be specified. If any of them is null, then it will be inferred from the error stack trace as described below.

Recommended usage: explicit value for class and method names are preferred to automatic inference for the following reasons:

  • Automatic inference is not 100% reliable, since the Java Virtual Machine is free to omit stack frame in optimized code.
  • When an exception occurred in a private method used internally by a public method, we sometime want to log the warning for the public method instead, since the user is not expected to know anything about the existence of the private method. If a developer really want to know about the private method, the stack trace is still available anyway.

If the classe or method arguments are null, then the originating class name and method name are inferred from the given error using the first StackTraceElementfor which the class name is inside a package or sub-package of the same name than the logger name.

Example: if the logger name is "org.apache.sis.image", then this method will uses the first stack trace element where the fully qualified class name starts with "org.apache.sis.image" or "org.apache.sis.image.io", but not "org.apache.sis.imageio".
[中]发生意外错误时调用。此方法记录一条级别为#的消息,警告指定的记录器。可以选择指定原始类名和方法名。如果其中任何一个为null,则将从错误堆栈跟踪中推断,如下所述。
推荐用法:类和方法名称的显式值优于自动推断,原因如下:
*自动推断不是100%可靠的,因为Java虚拟机可以在优化代码中自由省略堆栈帧。
*当公共方法内部使用的私有方法中发生异常时,我们有时希望记录公共方法的警告,因为用户不需要知道私有方法的存在。如果开发人员真的想知道私有方法,堆栈跟踪仍然可用。
如果类或方法参数为null,则使用第一个StackTraceElements从给定错误推断原始类名和方法名,该类名称位于与记录器名称同名的包或子包中。
示例:如果记录器名称为“org.apache.sis.image”,则此方法将使用第一个堆栈跟踪元素,其中完全限定的类名以“org.apache.sis.image”或“org.apache.sis.image.io”开头,而不是以“org.apache.sis.imageio”开头。

代码示例

代码示例来源:origin: Geomatys/geotoolkit

/**
   * Invoked when an unexpected exception occurred in the {@link #format} method.
   */
  private static void unexpectedException(final RuntimeException exception) {
    Logging.unexpectedException(null, IndexedResourceBundle.class, "format", exception);
  }
}

代码示例来源:origin: Geomatys/geotoolkit

/**
 * Returns the expected class for values stored under this key.
 *
 * @return The class of values stored under this key.
 */
public Class<?> getValueClass() {
  if (valueClass == null) {
    try {
      valueClass = Class.forName(className);
    } catch (ClassNotFoundException exception) {
      Logging.unexpectedException(null, Key.class, "getValueClass", exception);
      valueClass = Object.class;
    }
  }
  return valueClass;
}

代码示例来源:origin: org.apache.sis.core/sis-utility

/**
 * Invoked when a recoverable error occurred. This method is similar to
 * {@link #unexpectedException(Logger,Class,String,Throwable) unexpectedException(…)}
 * except that it does not log the stack trace and uses a lower logging level.
 *
 * @param  logger  where to log the error, or {@code null} for inferring a default value from other arguments.
 * @param  classe  the class where the error occurred, or {@code null} for inferring a default value from other arguments.
 * @param  method  the method name where the error occurred, or {@code null} for inferring a default value from other arguments.
 * @param  error   the error, or {@code null} if none.
 * @return {@code true} if the error has been logged, or {@code false} if the given {@code error}
 *         was null or if the logger does not log anything at {@link Level#FINE}.
 *
 * @see #unexpectedException(Logger, Class, String, Throwable)
 * @see #severeException(Logger, Class, String, Throwable)
 */
public static boolean recoverableException(final Logger logger, final Class<?> classe,
                      final String method, final Throwable error)
{
  final String classname = (classe != null) ? classe.getName() : null;
  return unexpectedException(logger, classname, method, error, Level.FINE);
}

代码示例来源:origin: org.apache.sis.core/sis-referencing

/**
   * Invoked when an unexpected exception occurred. Those exceptions must be non-fatal, i.e. the caller
   * <strong>must</strong> have a reasonable fallback (otherwise it should propagate the exception).
   */
  private static void unexpectedException(final String methodName, final Exception exception) {
    Logging.unexpectedException(Logging.getLogger(Modules.REFERENCING), CRS.class, methodName, exception);
  }
}

代码示例来源:origin: org.apache.sis.core/sis-referencing

/**
 * Logs a warning about an unexpected but non-fatal exception.
 *
 * @param method     the source method.
 * @param exception  the exception to log.
 */
private static void unexpectedException(final String method, final Exception exception) {
  Logging.unexpectedException(Logging.getLogger(Loggers.CRS_FACTORY), EPSGDataAccess.class, method, exception);
}

代码示例来源:origin: apache/sis

/**
   * Invoked when an unexpected exception occurred. Those exceptions must be non-fatal, i.e. the caller
   * <strong>must</strong> have a reasonable fallback (otherwise it should propagate the exception).
   */
  private static void unexpectedException(final String methodName, final Exception exception) {
    Logging.unexpectedException(Logging.getLogger(Modules.REFERENCING), CRS.class, methodName, exception);
  }
}

代码示例来源:origin: apache/sis

/**
 * Logs a warning about an unexpected but non-fatal exception.
 *
 * @param method     the source method.
 * @param exception  the exception to log.
 */
private static void unexpectedException(final String method, final Exception exception) {
  Logging.unexpectedException(Logging.getLogger(Loggers.CRS_FACTORY), EPSGDataAccess.class, method, exception);
}

代码示例来源:origin: org.apache.sis.core/sis-referencing

/**
 * Invoked when an exception occurred while closing a factory and we can not propagate the exception to the user.
 * This situation happen when the factories are closed in a background thread. There is not much we can do except
 * logging the problem. {@code ConcurrentAuthorityFactory} should be able to continue its work normally since the
 * factory that we failed to close will not be used anymore.
 *
 * @param  method     the name of the method to report as the source of the problem.
 * @param  exception  the exception that occurred while closing a Data Access Object.
 */
static void unexpectedException(final String method, final Exception exception) {
  Logging.unexpectedException(Logging.getLogger(Loggers.CRS_FACTORY),
      ConcurrentAuthorityFactory.class, method, exception);
}

代码示例来源:origin: apache/sis

/**
 * Invoked when an exception occurred while closing a factory and we can not propagate the exception to the user.
 * This situation happen when the factories are closed in a background thread. There is not much we can do except
 * logging the problem. {@code ConcurrentAuthorityFactory} should be able to continue its work normally since the
 * factory that we failed to close will not be used anymore.
 *
 * @param  method     the name of the method to report as the source of the problem.
 * @param  exception  the exception that occurred while closing a Data Access Object.
 */
static void unexpectedException(final String method, final Exception exception) {
  Logging.unexpectedException(Logging.getLogger(Loggers.CRS_FACTORY),
      ConcurrentAuthorityFactory.class, method, exception);
}

代码示例来源:origin: org.apache.sis.core/sis-referencing

/**
   * Logs a warning about a non-invertible transform. This method may be invoked during WKT
   * formatting. This error should never occur, but it still possible to recover from this
   * error and let WKT formatting to continue, which can be useful for debugging.
   *
   * <p>We pretend that the error come from {@link ConcatenatedTransform#formatTo(Formatter)}
   * because this error should occurs only in the context of WKT formatting of a concatenated
   * transform.</p>
   */
  private static void unexpectedException(final IllegalStateException e) {
    Logging.unexpectedException(Logging.getLogger(Loggers.WKT), ConcatenatedTransform.class, "formatTo", e.getCause());
  }
}

代码示例来源:origin: Geomatys/geotoolkit

/**
 * Shutdown the database represented by this data source.
 *
 * @since 3.03
 */
public void shutdown() {
  final Dialect dialect = Dialect.forURL(url);
  if (dialect != null) try {
    dialect.shutdown(null, url, false);
  } catch (SQLException e) {
    Logging.unexpectedException(LOGGER, DefaultDataSource.class, "shutdown", e);
  }
}

代码示例来源:origin: apache/sis

/**
   * Logs a warning about a non-invertible transform. This method may be invoked during WKT
   * formatting. This error should never occur, but it still possible to recover from this
   * error and let WKT formatting to continue, which can be useful for debugging.
   *
   * <p>We pretend that the error come from {@link ConcatenatedTransform#formatTo(Formatter)}
   * because this error should occurs only in the context of WKT formatting of a concatenated
   * transform.</p>
   */
  private static void unexpectedException(final IllegalStateException e) {
    Logging.unexpectedException(Logging.getLogger(Loggers.WKT), ConcatenatedTransform.class, "formatTo", e.getCause());
  }
}

代码示例来源:origin: Geomatys/geotoolkit

/**
   * Closes the JDBC connection used by this factory.
   */
  @Override
  protected synchronized void dispose(final boolean shutdown) {
    try {
      ((SpatialRefSysMap) definitions).dispose();
    } catch (SQLException exception) {
      Logging.unexpectedException(null, DirectPostgisFactory.class, "dispose", exception);
    }
    authority   = null;
    authorities = null;
    super.dispose(shutdown);
  }
}

代码示例来源:origin: apache/sis

/**
   * Invoked indirectly by the garbage collector.
   */
  @Override
  public void dispose() {
    try {
      close();
    } catch (SQLException exception) {
      /*
       * There is nothing we can do here. It is not even worth to throw an unchecked exception because
       * this method is invoked from a background thread, so the exception would not reach user's code.
       * Pretend that the logging come from AuthorityCodes because it is closer to a public API (or at
       * least, easier to guess that it is related to the EPSGDataAccess.getAuthorityCodes() method).
       */
      Logging.unexpectedException(Logging.getLogger(Loggers.CRS_FACTORY), AuthorityCodes.class, "close", exception);
    }
  }
}

代码示例来源:origin: org.apache.sis.core/sis-referencing

/**
   * Invoked indirectly by the garbage collector.
   */
  @Override
  public void dispose() {
    try {
      close();
    } catch (SQLException exception) {
      /*
       * There is nothing we can do here. It is not even worth to throw an unchecked exception because
       * this method is invoked from a background thread, so the exception would not reach user's code.
       * Pretend that the logging come from AuthorityCodes because it is closer to a public API (or at
       * least, easier to guess that it is related to the EPSGDataAccess.getAuthorityCodes() method).
       */
      Logging.unexpectedException(Logging.getLogger(Loggers.CRS_FACTORY), AuthorityCodes.class, "close", exception);
    }
  }
}

代码示例来源:origin: org.apache.sis.core/sis-metadata

/**
   * Invoked if JNDI lost connection to the server while preparing the {@code NamingEvent}.
   * Clears the data source anyway. In the worst case scenario, the application will fetch
   * it again from a the JNDI context.
   */
  @Override
  public void namingExceptionThrown(NamingExceptionEvent event) {
    Logging.unexpectedException(Logging.getLogger(Loggers.SYSTEM),
        Listener.class, "namingExceptionThrown", event.getException());
    objectChanged(null);
  }
}

代码示例来源:origin: apache/sis

/**
   * Invoked if JNDI lost connection to the server while preparing the {@code NamingEvent}.
   * Clears the data source anyway. In the worst case scenario, the application will fetch
   * it again from a the JNDI context.
   */
  @Override
  public void namingExceptionThrown(NamingExceptionEvent event) {
    Logging.unexpectedException(Logging.getLogger(Loggers.SYSTEM),
        Listener.class, "namingExceptionThrown", event.getException());
    objectChanged(null);
  }
}

代码示例来源:origin: org.apache.sis.core/sis-referencing

/**
   * Invoked when the data source changed.
   */
  @Override
  protected void dataSourceChanged() {
    try {
      ((MultiAuthoritiesFactory) CRS.getAuthorityFactory(null)).reload();
    } catch (FactoryException e) {
      // Should never happen for a null argument given to CRS.getAuthorityFactory(…).
      Logging.unexpectedException(Logging.getLogger(Loggers.CRS_FACTORY), CRS.class, "getAuthorityFactory", e);
    }
  }
}

代码示例来源:origin: apache/sis

/**
   * Invoked when the data source changed.
   */
  @Override
  protected void dataSourceChanged() {
    try {
      ((MultiAuthoritiesFactory) CRS.getAuthorityFactory(null)).reload();
    } catch (FactoryException e) {
      // Should never happen for a null argument given to CRS.getAuthorityFactory(…).
      Logging.unexpectedException(Logging.getLogger(Loggers.CRS_FACTORY), CRS.class, "getAuthorityFactory", e);
    }
  }
}

代码示例来源:origin: apache/sis

/**
 * Returns the version number of the {@literal Proj.4} library.
 * Returns {@code null} if Proj.4 is not installed on the current system.
 *
 * <div class="note"><b>Example:</b> Rel. 4.9.3, 15 August 2016</div>
 *
 * @return the Proj.4 release string, or {@code null} if the native library has been found.
 */
public static String version() {
  try {
    return PJ.getRelease();
  } catch (UnsatisfiedLinkError e) {
    // Thrown the first time that we try to use the library.
    Logging.unexpectedException(Logging.getLogger(Modules.GDAL), Proj4.class, "version", e);
  } catch (NoClassDefFoundError e) {
    // Thrown on all attempts after the first one.
    Logging.recoverableException(Logging.getLogger(Modules.GDAL), Proj4.class, "version", e);
  }
  return null;
}

相关文章