cosmic_dust 2011-01-06 21:47
浏览 326
已采纳

有个java 异常问题不解。。

[code="java"]

public class ClassA {

public ClassA()throws Exception{
    // TODO Auto-generated constructor stub

    try{
        throw new  RuntimeException("bbbbbbbbbb");
    }catch(RuntimeException e){
        throw new Exception("cccccccc"+ e.getMessage(),e);
    }
}

}

[/code]

[code="java"]
public class CatchExceptionTest {

public static void main(String[] args){

    try{
        Class throttleClass = Class.forName("ClassA");
        Constructor c = throttleClass.getConstructor();
        ClassA a = (ClassA)c.newInstance();

    }catch(RuntimeException ee){
        System.out.println("11111111");
        System.out.println(ee.getMessage());
    }catch(Exception e){
        System.out.println("222222222 " + e.toString());
        System.out.println(e.getMessage());
    }
}

}

[/code]

求主方法执行的结果, 以及出现的原因。。。

  • 写回答

19条回答 默认 最新

  • ageofnodoubt 2011-01-07 11:45
    关注

    [code="java"]

    /**

    • 自定义异常类
    • */
      public class CustomException extends Exception {

      /** 自定义错误代码 */
      private String errorCode;

      public CustomException() {
      super();
      }

      public CustomException(String message, Throwable cause) {
      super(message, cause);
      }

      public CustomException(String message) {
      super(message);
      }

      public CustomException(Throwable cause) {
      super(cause);
      }

      /**

      • 添加一个自定义的code属性
      • @param errorCode
      • @param message
      • @param cause */ public CustomException(String errorCode, String message, Throwable cause) { super(message, cause); this.errorCode = errorCode; }

      public String getErrorCode() {
      return errorCode;
      }

      public void setErrorCode(String errorCode) {
      this.errorCode = errorCode;
      }

    }

    [/code]

    // 构造异常
    new CustomException("errorA", "异常A", e);
    new CustomException("errorB", "异常B", e);

    捕捉:
    try {
    ....
    } catch (CustomException ce) {
    System.out.println(ce.getErrorCode());
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(18条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题