写一个方法,接受数组作为参数: 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); } }可能不是你的要求,你类似写一个吧
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]);
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 那个地方不对?请告诉我,谢谢!!
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); } } 我的代码几乎和你的一样,我可以编译成功啊,为什么你的不行呢?好怪啊!
你那个MyException是加到了哪个地方,怎么写的?能不能贴出来?
MyException.java和TestException.java放在同一个目录下:内容如下 class MyException extends IndexOutOfBoundsException{ public MyException(){ super(); } }先将这个类编译,在编译TestException.java,编译通过,如果执行时不代参数就会抛出异常!
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);
}
}可能不是你的要求,你类似写一个吧
public NoparameterException(String str){
super(str);
}
}上面是你自己写的类。在你的程序中if(args[0] == null)
throw new NoParameterException("No Parameter inputs.");
else
i=Integer.parseInt(args[0]);
{
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
那个地方不对?请告诉我,谢谢!!
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);
}
}
我的代码几乎和你的一样,我可以编译成功啊,为什么你的不行呢?好怪啊!
class MyException extends IndexOutOfBoundsException{
public MyException(){
super();
}
}先将这个类编译,在编译TestException.java,编译通过,如果执行时不代参数就会抛出异常!
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]="";
} }
}
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,可以这样啊
java triangle 12
这都是很基本的啊
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)
这个结果应该是对的吧