请问一下,我这个Test程序那里出错了?

我想飞走 2012-03-29 11:49:10

public class Test{
public static void main(String[] args){
Test TT = new Test();
try{
//TT.num(-1);
}catch (MyExceptions1 ME1){
System.out.println(ME1.getMessage());
}catch (MyExceptions2 ME2){
System.out.println(ME2.getMessage());
}
}

void num1(int i) throws MyException1{
if(i < 0){
throw new MyException1("数字太小;");
}
}

void num2(int i) throws MyExceptions2{
if(i > 0){
throw new MyException2("数字太大;");
}
}


class MyException1 extends Exception{
//private int id = 1;
public MyException1(String s){
super("数字太小;");
}
}

class MyException2 extends Exception{
//private int id = 2;
public MyException2(String s){
super("数字太大;");
}
}
}



总是报错,有人能帮我解决下么?
...全文
69 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdojqy1122 2012-03-29
  • 打赏
  • 举报
回复

public class bb{
public static void main(String[] args){
bb TT = new bb();
try{
TT.num(-1);
}catch (MyException1 ME1){
System.out.println(ME1.getMessage());
}catch (MyException2 ME2){
System.out.println(ME2.getMessage());
}
}

void num(int i) throws MyException1,MyException2{//这样不是更好?
if(i < 0){
throw new MyException1("数字太小;");
}
if(i > 0){
throw new MyException2("数字太大;");
}
}
class MyException1 extends Exception{
//private int id = 1;
public MyException1(String s){
super("数字太小;");
}
}

class MyException2 extends Exception{
//private int id = 2;
public MyException2(String s){
super("数字太大;");
}
}
}
宏Lee 2012-03-29
  • 打赏
  • 举报
回复
你要的应该是

public class Test {
public static void main(String[] args){
Test TT = new Test();
try{
TT.num1(-1);
TT.num2(1);
}catch (MyException1 ME1){
System.out.println(ME1.getMessage());
}catch (MyException2 ME2){
System.out.println(ME2.getMessage());
}
}

void num1(int i) throws MyException1{
if(i < 0){
throw new MyException1("数字太小;");
}
}

void num2(int i) throws MyException2{
if(i > 0){
throw new MyException2("数字太大;");
}
}


class MyException1 extends Exception{
//private int id = 1;
public MyException1(String s){
super("数字太小;");
}
}

class MyException2 extends Exception{
//private int id = 2;
public MyException2(String s){
super("数字太大;");
}
}
}
宏Lee 2012-03-29
  • 打赏
  • 举报
回复
共有2个问题

public class Test{
public static void main(String[] args){
Test TT = new Test();
try{
//TT.num(-1);
}catch (MyExceptions1 ME1){
System.out.println(ME1.getMessage());
}catch (MyExceptions2 ME2){
System.out.println(ME2.getMessage());
}

catch 是处理异常的,你连异常都没有,干嘛catch ?
}

void num1(int i) throws MyException1{
if(i < 0){
throw new MyException1("数字太小;");
}
}

void num2(int i) throws MyExceptions2你干嘛多写一个s{
if(i > 0){
throw new MyException2("数字太大;");
}
}


class MyException1 extends Exception{
//private int id = 1;
public MyException1(String s){
super("数字太小;");
}
}

class MyException2 extends Exception{
//private int id = 2;
public MyException2(String s){
super("数字太大;");
}
}
}
代号裤子 2012-03-29
  • 打赏
  • 举报
回复
MyExceptions1和MyExceptions2 拼写错误
五哥 2012-03-29
  • 打赏
  • 举报
回复


class MyException1 extends Exception{
//private int id = 1;
public MyException1(String s){
super("数字太小;");
}
}

class MyException2 extends Exception{
//private int id = 2;
public MyException2(String s){
super("数字太大;");
}
}
public class Test4{
public static void main(String[] args){
Test4 TT = new Test4();
//TT.num1(-1);
try{
TT.num1(-1);
TT.num2(1) ; //如果不写这个的话 ,系统会认为你的这个try不会抛出MyException2 ,不应该有catch(MyException2 ME2)这个部分 }catch (MyException1 ME1) {//MyExceptions1 名字写错啦 多了个s System.out.println(ME1.getMessage());

}catch (MyException2 ME2){//MyExceptions1 名字写错啦 多了个s System.out.println(ME2.getMessage());
}

}

void num1(int i)throws MyException1{
if(i < 0){
throw new MyException1("数字太小;");
}
}

void num2(int i) throws MyException2{//MyExceptions1 名字写错啦 多了个s if(i > 0){
throw new MyException2("数字太大;");
}
}


}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧