in class B:private void CallMethodInA()
{
try
{
new B().MethodName();
}
catch(Exception ex)
{
//异常处理,如写入日志等
}
}

解决方案 »

  1.   

    使用log4j
    ...
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    ...
    class B {
        protected Log log = null;
        public B() {
            log = LogFactory.getLog(this.getClass());
        }
    ...
        try {
            A a = new A();
            a.someMethod();
        }
        catch (Exception e) {
            log.error(e);
        }
    }其它详见
    http://blog.csdn.net/changzheng/archive/2004/12/01/201098.aspx
      

  2.   

    日志文件需要log4j.jar和common-logger.jar,
    log4j.properties
    # For JBoss: Avoid to setup log4j outside $JBOSS_HOME/server/default/deploy/log4j.xml!
    # For all other servers: Comment out the Log4J listener in web.xml too.
    log4j.rootLogger=INFO, stdout, logfile
     
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
    log4j.appender.logfile=org.apache.log4j.RollingFileAppender
    log4j.appender.logfile.File=springapp.log
    log4j.appender.logfile.MaxFileSize=512KB
    # Keep three backup files.
    log4j.appender.logfile.MaxBackupIndex=3
    # Pattern to output: date priority [category] - message
    log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
    log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
    in class B:protected final Log logger = LogFactory.getLog(getClass());
    private void CallMethodInA()
    {
    try
    {
    new B().MethodName();
    }
    catch(Exception ex)
    {
    logger.info(ex.getMessage());
    }
    }