当前位置: 代码迷 >> Java相关 >> [求助]谁能帮我看一下,为什么不能得到我想要的自定义异常呢
  详细解决方案

[求助]谁能帮我看一下,为什么不能得到我想要的自定义异常呢

热度:138   发布时间:2005-09-28 17:36:00.0
[求助]谁能帮我看一下,为什么不能得到我想要的自定义异常呢

谁能告诉我下面的程序为什么不能得到我想要的结果呀? 我希望:输入的字符,如果不能转化为数字,则将引发我自定义的异常,看看我写的这个程序有什么问题,指点指点小弟我呀 class NumberFormatExceptionDemo extends NumberFormatException { NumberFormatExceptionDemo() { super("输入无效字符"); } }

class Input { int num;

Input(int num) { this.num = num; System.out.println("num:" + num); }

public static void main(String args[]) { try { new Input(Integer.parseInt(args[0])); }

catch (NumberFormatExceptionDemo e) { System.out.println(e); } } };

搜索更多相关的解决方案: 定义  

----------------解决方案--------------------------------------------------------
class NumberFormatExceptionDemo
{
  String input;
  public NumberFormatExceptionDemo(String s)
  {
    input = s;
  }
  public String toString()
  {
    return "输入无效字符 " + input;
  }
}

class Input
{
  int num;

  Input(int num)
  {
    this.num = num;
    System.out.println("num:" + num);
  }

  public static void main(String args[])
  {
    try
    {
      if(args.length != 0)
        new Input(Integer.parseInt(args[0]));
    }
    catch (NumberFormatException e)
    {
      NumberFormatExceptionDemo nfed = new NumberFormatExceptionDemo(args[0]);
      System.err.println(nfed.toString());
    }
  }
}
----------------解决方案--------------------------------------------------------
  相关解决方案