「java除零异常」java除数为零

博主:adminadmin 2022-12-28 15:36:09 52

今天给各位分享java除零异常的知识,其中也会对java除数为零进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

JAVA除0异常抛出错误了

super("除数为0异常"); Container container = getContentPane(); container.setLayout(new GridLayout(3, 2)); container.add(new JLabel("输入被除数", SwingConstants.RIGHT)); numeratorField = new JTextField(10); container.add(numeratorField); container.add(new JLabel("输入除数并回车", SwingConstants.RIGHT)); denominatorField = new JTextField(10); container.add(denominatorField); denominatorField.addActionListener(this); container.add(new JLabel("结果", SwingConstants.RIGHT)); outputField = new JTextField(); container.add(outputField); setSize(425, 100); setVisible(true);}public void actionPerformed(ActionEvent event) { outputField.setText(""); numerator = Integer.parseInt(numeratorField.getText());// 被除数try {denominator = Integer.parseInt(denominatorField.getText()); } catch (NumberFormatException ex) {// 捕捉除数格式异常错误 System.out.println("I detected Exception" + ex.toString()); return;// 当发现异常的时候退出方法}try {/** 这个地方调用quotient()方法,则必须要捕捉异常*/quotient = quotient(numerator, denominator); } catch (myArithmeticException e) { // TODO Auto-generated catch block System.out.println(e.toString()); outputField.setText(e.toString());return;}outputField.setText(Integer.toString(quotient));}/** 这个方法抛出异常,那么调用这个方法是就要捕捉异常*/public int quotient(int numerator, int deniminator) throws myArithmeticException { if (denominator == 0) throw new myArithmeticException(denominator); return numerator / deniminator;}public static void main(String[] args) { // TODO Auto-generated method stub

JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意

throw 仅用于方法定义后面,指示该方法可能会抛出什么异常,使用该方法的方法必须处理该异常,或者再次抛出。

throws 用于当程序判断发生异常时,用该语句抛出异常,或处理异常时再次抛出异常。

//下面三个关键字就是处理异常

try {

//这里放可能会发生异常的语句

} catch(Exception e) {

//这里处理异常

} finally {

//这里的语句必然会得到执行,不管异常发省与否,

//用于关闭数据库,关闭连接等收尾操作(非必要)

}

java异常的一个简单例子,比如我有一个除法方法

public int divide(int a, int b) {

return a / b;

}

但是这就有一个问题,当b是0时,程序会报错。

如果引入异常,改成这样

public int divide(int a, int b) throws Exception {

if (b == 0) {

throw new Exception("b = 0");

}

return a / b;

}

那么调用端该怎么用呢

public class ExceptionTest {

public static void main(String[] args) {

ExceptionTest et = new ExceptionTest();

try {

System.out.println(et.divide(12, 0));

} catch (Exception e) {

System.out.println("0不能做被除数");

}

}

public int divide(int a, int b) throws Exception {

if (b == 0) {

throw new Exception("b = 0");

}

return a / b;

}

}

程序可以继续执行,不会中断。

Java 除零异常用Exception捕获为什么会提示不兼容的类型?

因为你的这个测试类叫做Exception ,编译器认为要抛出这个类,但是这个类又不是异常,因为没有继承exception。修改类名再测试、

JAVA中0除以一个数会有什么异常

程序中的每个除数,都要处理 其为0的可能性.可以强制改为一个合法的数,或者函数直接返回.

也可返回一个自己的异常.

(1):double d=0.0   其实d的值只是无线接近于0而已。

(2):int iWidth = m_rectmin.Width();  

if(iWidth==0) iWidth = 1;

(3):Structured Exception Handling 异常扑捉 .

Java中为什么整数除以0出现异常

这种问题写个代码跑一下就可以了,java中整数除以0会引发

java.lang.ArithmeticException: / by zero

ArithmeticException是出现异常的运算条件时,抛出此异常,结果如下图:

除此之外还要注意,浮点数除以0将会的得到 Infinity(无穷大)或NaN结果,但不会报异常。

Java程序填空。下面程序的功能是产生一个除数为0的异常并捕获,请填空使程序完整

1): throws ArithmeticException

2:)new

3:)ArithmeticException

代码块里抛异常使用的是throw 关键字, 方法体抛异常使用的是throws 关键字

使用try catch 用于捕获抛出的异常, 并试图进行处理

完整的参考代码

public class TestThrow {

static int x = 0;

static int y = 0;

public static void divide() throws ArithmeticException { // 方法定义抛出异常

if (x != 0) {

y = 100 / x;

} else {

throw new ArithmeticException("除数不能为零!");//抛出异常

}

}

public static void main(String[] args) {

try {

divide();

} catch (ArithmeticException e) {//捕获方法抛出的异常,并在catch里打印出来

e.printStackTrace();

}

}

}

java除零异常的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java除数为零、java除零异常的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-28,除非注明,否则均为首码项目网原创文章,转载请注明出处。