一Java小程序出现异常
下面是我写的程序classMyException2extendsException{/**thesecondmyexception*/MyException2(Strin...
下面是我写的程序
class MyException2 extends Exception{
/**the second myexception
*/
MyException2(String s){
super(s);
System.out.println("that's my exception");
}
public class TestException{
public int add(int a,int b) throws MyException2{
int c=a+b;
if (c>999) throw new MyException2("c is too big");
else return c;
}
}
public static void main(String[] args) {
TestException test=new TestException();
try{
int[] i=new int[2];
test.add(i[0],i[1]);
}catch(MyException2 e){
e.printStackTrace();
}
}
}
在这一句TestException test=new TestException();出现了错误,提示说No enclosing instance of type MyException2 is accessible. Must qualify the allocation with an enclosing instance of type MyException2 (e.g. x.new A() where x is an instance of MyException2).
请帮忙看下,十分感谢。 展开
class MyException2 extends Exception{
/**the second myexception
*/
MyException2(String s){
super(s);
System.out.println("that's my exception");
}
public class TestException{
public int add(int a,int b) throws MyException2{
int c=a+b;
if (c>999) throw new MyException2("c is too big");
else return c;
}
}
public static void main(String[] args) {
TestException test=new TestException();
try{
int[] i=new int[2];
test.add(i[0],i[1]);
}catch(MyException2 e){
e.printStackTrace();
}
}
}
在这一句TestException test=new TestException();出现了错误,提示说No enclosing instance of type MyException2 is accessible. Must qualify the allocation with an enclosing instance of type MyException2 (e.g. x.new A() where x is an instance of MyException2).
请帮忙看下,十分感谢。 展开
展开全部
把最后一个大括号移到System.out.println("that's my exception");
} 后,变为:
System.out.println("that's my exception");
}
}
缺少默认的构造函数,把
class MyException2 extends Exception{
/**the second myexception
*/
MyException2(String s){
super(s);
System.out.println("that's my exception");
}
}
改为:
class MyException2 extends Exception{
/**the second myexception
*/
MyException2(String s){
super(s);
System.out.println("that's my exception");
}
MyException2(){}
}
} 后,变为:
System.out.println("that's my exception");
}
}
缺少默认的构造函数,把
class MyException2 extends Exception{
/**the second myexception
*/
MyException2(String s){
super(s);
System.out.println("that's my exception");
}
}
改为:
class MyException2 extends Exception{
/**the second myexception
*/
MyException2(String s){
super(s);
System.out.println("that's my exception");
}
MyException2(){}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
两个类文件分开写,
MyException2 .java
class MyException2 extends Exception{
/**the second myexception
*/
MyException2(String s){
super(s);
System.out.println("that's my exception");
public static void main(String[] args) {
TestException test=new TestException();
try{
int[] i=new int[2];
test.add(i[0],i[1]);
}catch(MyException2 e){
e.printStackTrace();
}
}
}
TestException.java
public class TestException{
public int add(int a,int b) throws MyException2{
int c=a+b;
if (c>999) throw new MyException2("c is too big");
else return c;
}
}
是没有任何异常信息。但不知道你要什么结果了
MyException2 .java
class MyException2 extends Exception{
/**the second myexception
*/
MyException2(String s){
super(s);
System.out.println("that's my exception");
public static void main(String[] args) {
TestException test=new TestException();
try{
int[] i=new int[2];
test.add(i[0],i[1]);
}catch(MyException2 e){
e.printStackTrace();
}
}
}
TestException.java
public class TestException{
public int add(int a,int b) throws MyException2{
int c=a+b;
if (c>999) throw new MyException2("c is too big");
else return c;
}
}
是没有任何异常信息。但不知道你要什么结果了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这样该就可以了
MyException2.java
class MyException2 extends Exception{
/**the second myexception
*/
MyException2(String s){
super(s);
System.out.println("that's my exception");
}
public static void main(String[] args) {
TestException test=new TestException();
try{
int[] i=new int[2];
test.add(999,2);
}catch(MyException2 e){
e.printStackTrace();
}
}
}
class TestException{
public int add(int a,int b) throws MyException2{
int c=a+b;
if (c>999) throw new MyException2("c is too big");
else return c;
}
}
MyException2.java
class MyException2 extends Exception{
/**the second myexception
*/
MyException2(String s){
super(s);
System.out.println("that's my exception");
}
public static void main(String[] args) {
TestException test=new TestException();
try{
int[] i=new int[2];
test.add(999,2);
}catch(MyException2 e){
e.printStackTrace();
}
}
}
class TestException{
public int add(int a,int b) throws MyException2{
int c=a+b;
if (c>999) throw new MyException2("c is too big");
else return c;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
else return c;
}
后面的大括号删除。
}
后面的大括号删除。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你{ }的范围有点问题
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询