自己写一个类继承Exception或者实现Throwable接口:
eg: class MyException extends Exception{
       public MyException(){
                super();
            }
      }

解决方案 »

  1.   

    这一步已经做完了,剩下的我应该怎么做呢?在main中怎么写?请指教!!
      

  2.   

    说错了,应该是继承Throwable类!
      

  3.   

    写一个方法,接受数组作为参数:
    public class TestException{
    public void TestException(){
    }
    public void parse(Object a[]){
    if(a.length<1)
    throw new MyException();
    else
    System.out.println("no exception here");
    }
    public static void main(String args[])throws MyException{
    TestException te=new TestException();
    te.parse(args);
    }
    }可能不是你的要求,你类似写一个吧
      

  4.   

    public class NoParameterException extends Exception{
           public NoparameterException(String str){
                    super(str);
                }
    }上面是你自己写的类。在你的程序中if(args[0] == null)
      throw new NoParameterException("No Parameter inputs.");
    else 
      i=Integer.parseInt(args[0]);
      

  5.   

    public class triangle
    {
    class MyException extends Exception
    {
            public MyException()
            {
                     super();
                 }
           }
    public void triangle(){
    }

    public void parse(Object a[])
    {
    if(a.length<1)
    throw new MyException();
    else
    System.out.println("no exception here");
    } public static void  main(String args[]) throws MyException
    {
    int i,j,t;
    String str[]={"",""};
    i=0;
    j=0;
    t=0;
    triangle te=new triangle();
    te.parse(args);
    i=Integer.parseInt(args[0]);
    for(j=0;j<i;j++)
    {
    for(t=0;t<i-j;t++)
    {
    str[0]=str[0]+" ";
    }
    for(t=0;t<2*j-1;t++)
    {
    str[1]=str[1]+"*";
    }
    System.out.println(str[0]+str[1]+str[0]);
    str[0]="";
    str[1]="";
    } }
    }
    这是我的程序,编译时报错如下
    F:\java>javac triangle.java
    triangle.java:16: unreported exception triangle.MyException; must be caught or d
    eclared to be thrown
                            throw new MyException();
                            ^
    1 error
    那个地方不对?请告诉我,谢谢!!
      

  6.   

    public class TestException{
    public void TestException(){
    }
    public void parse(Object a[]){
    if(a.length<1)
    throw new MyException();
    else
    System.out.println("no exception here");
    }
    public static void main(String args[])throws MyException{
    TestException te=new TestException();
    te.parse(args);
    }
    }
    我的代码几乎和你的一样,我可以编译成功啊,为什么你的不行呢?好怪啊!
      

  7.   

    你那个MyException是加到了哪个地方,怎么写的?能不能贴出来?
      

  8.   

    MyException.java和TestException.java放在同一个目录下:内容如下
    class MyException extends IndexOutOfBoundsException{
      public MyException(){
        super();
    }
    }先将这个类编译,在编译TestException.java,编译通过,如果执行时不代参数就会抛出异常!
      

  9.   

    那就是了,我的老师要求,自定义的异常不能从IndexOutOfBoundsException继承,是个独立的异常,而且都是在一个文件里
      

  10.   

    这样也好办啊,你将你的代码改成下面的代码:
    public class triangle
    {
    class MyException extends Exception
    {
            public MyException()
            {
                     super();
                 }
           }
    public void triangle(){
    }

    public void parse(Object a[])throws MyException
    {
    if(a.length<1)
    throw new MyException();
    else
    System.out.println("no exception here");
    } public static void  main(String args[]) throws MyException
    {
    int i,j,t;
    String str[]={"",""};
    i=0;
    j=0;
    t=0;
    triangle te=new triangle();
    te.parse(args);
    i=Integer.parseInt(args[0]);
    for(j=0;j<i;j++)
    {
    for(t=0;t<i-j;t++)
    {
    str[0]=str[0]+" ";
    }
    for(t=0;t<2*j-1;t++)
    {
    str[1]=str[1]+"*";
    }
    System.out.println(str[0]+str[1]+str[0]);
    str[0]="";
    str[1]="";
    } }
    }
      

  11.   

    编译是通过了,但是在运行的时候报错
    F:\java>java sanjiao
    Exception in thread "main" java.lang.NoClassDefFoundError: sanjiaoF:\java>java sanjiao 12
    Exception in thread "main" java.lang.NoClassDefFoundError: sanjiao
      

  12.   

    你怎么能这样运行呢?
    如果要传递参数12,可以这样啊
    java triangle 12
    这都是很基本的啊
      

  13.   

    呵呵,不好意思
    F:\java>java triangle  12
    no exception here           *
              ***
             *****
            *******
           *********
          ***********
         *************
        ***************
       *****************
      *******************
     *********************F:\java>java triangle
    Exception in thread "main" triangle$MyException
            at triangle.parse(triangle.java:17)
            at triangle.main(triangle.java:30)
    这个结果应该是对的吧