新手求解java异常问题

mikilo 2019-04-20 05:46:23


在throw new Exception()里设置了“除数不能为0”,为什么输出的是/by zero?请问怎样设置能获取到throw new Exception()里面的信息。
...全文
507 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
mikilo 2019-05-01
  • 打赏
  • 举报
回复
各位大佬的意见都看了,感谢回答😁
qq_23874291 2019-04-26
  • 打赏
  • 举报
回复
你第一步就int b=a/b,这个b为 0,你程序不就是直接跪了么
WongJP 2019-04-24
  • 打赏
  • 举报
回复
引用 7 楼 weixin_44755049 的回复:
[quote=引用 6 楼 WongJP 的回复:]
public class Test {
    private int a;
    private int b;

    public Test(int a, int b){
        this.a = a;
        this.b = b;
    }

    public int division()throws Exception{
        if (b == 0) {
            throw new Exception("除数不能为0!");
        } else {
            int resultNum = a/b;
            return resultNum;
        }
    }

    public static void main(String[] args) {
        Test test = new Test(1,5);
        try{
            System.out.println(test.division());
        }catch(Exception e){
            e.getMessage();
        }
    }
}
第一点:你连最基本的封装都没学会 第二点:你这样是在浪费内存,虽然万物皆对象,但不等于把简单问题复杂化[/quote] 还请指教一下这块您写会怎么写,我学习一下
WongJP 2019-04-23
  • 打赏
  • 举报
回复
public class Test {
    private int a;
    private int b;

    public Test(int a, int b){
        this.a = a;
        this.b = b;
    }

    public int division()throws Exception{
        if (b == 0) {
            throw new Exception("除数不能为0!");
        } else {
            int resultNum = a/b;
            return resultNum;
        }
    }

    public static void main(String[] args) {
        Test test = new Test(1,5);
        try{
            System.out.println(test.division());
        }catch(Exception e){
            e.getMessage();
        }
    }
}
苏洛洛Ar 2019-04-23
  • 打赏
  • 举报
回复
引用 6 楼 WongJP 的回复:
public class Test {
    private int a;
    private int b;

    public Test(int a, int b){
        this.a = a;
        this.b = b;
    }

    public int division()throws Exception{
        if (b == 0) {
            throw new Exception("除数不能为0!");
        } else {
            int resultNum = a/b;
            return resultNum;
        }
    }

    public static void main(String[] args) {
        Test test = new Test(1,5);
        try{
            System.out.println(test.division());
        }catch(Exception e){
            e.getMessage();
        }
    }
}
第一点:你连最基本的封装都没学会 第二点:你这样是在浪费内存,虽然万物皆对象,但不等于把简单问题复杂化
qq_39936465 2019-04-22
  • 打赏
  • 举报
回复
引用 楼主 mikilo 的回复:
在throw new Exception()里设置了“除数不能为0”,为什么输出的是/by zero?请问怎样设置能获取到throw new Exception()里面的信息。

public class test3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		test3 ff=new test3();
		try {
			ff.add(5, 0);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}

	}
	
	public void add(int a, int b)throws Exception{
		if(b==0) {
			throw new Exception("除数不能为0!");
		}else {
			System.out.println(a/b);
		}
	}

}

z_yemu 2019-04-20
  • 打赏
  • 举报
回复
引用 3 楼 z_yemu 的回复:
int c = a/b; 这个位置应该放在 if(b==0) 里面,你写 if(c==0) 是指商等于0的情况执行掏出一个Exception异常,而实际上,你b=0,在执行 c=a/b 时就产生异常了,异常信息就是 被除数不能为0,
说错了,是if(b!=0)
z_yemu 2019-04-20
  • 打赏
  • 举报
回复
int c = a/b; 这个位置应该放在 if(b==0) 里面,你写 if(c==0) 是指商等于0的情况执行掏出一个Exception异常,而实际上,你b=0,在执行 c=a/b 时就产生异常了,异常信息就是 被除数不能为0,
哦呼 2019-04-20
  • 打赏
  • 举报
回复
引用 1 楼 skyskni 的回复:
if 里面为什么不是b==0 ?

同上
LZ你自己都写了除数不能为零 为什么判断的是商?
  • 打赏
  • 举报
回复
if 里面为什么不是b==0 ?

62,614

社区成员

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

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