简体   繁体   English

线程“main”中的异常 java.lang.ArithmeticException: / 为零

[英]Exception in thread “main” java.lang.ArithmeticException: / by zero

I have two questions about Exceptions .我有两个关于Exceptions问题。 Firstly, I got this message from my code...首先,我从我的代码中得到了这条消息......

Exception in thread "main" java.lang.ArithmeticException: / by zero

This error message means dividing by zero, such as by doing int a = 5 / 0;这个错误信息的意思是除以零,比如做int a = 5 / 0;

A method can throw an Exception class instance, can't it?一个方法可以抛出一个Exception类实例,不是吗? But this is an expression.但这是一种表达。 Why can an expression throw an Exception class instance ?为什么一个表达式会抛出一个Exception类实例?

My second question is about thread "main" .我的第二个问题是关于thread "main" What is thread "main" ?什么是thread "main" Does "main" mean the static main method? "main"是指static main方法吗?

java.lang.ArithmeticException is a type of Exception that aims to better describe what the problem actually is. java.lang.ArithmeticException是一种Exception类型,旨在更好地描述问题的实际情况。 There's no point just creating an Exception , as it really could be caused by anything.仅仅创建一个Exception是没有意义的,因为它真的可能是由任何原因引起的。 By generating an ArithmeticException , the user can immediately know that the problem is something to do with a calculation.通过生成ArithmeticException ,用户可以立即知道问题与计算有关。 An Exception can be thrown by any piece of code, including in calculations such as your example.一个Exception可以通过任何一段代码被抛出,包括在计算中,如你的例子。

Exception in thread "main" means that the exception is thrown by the main() method, which also happens to be the primary Thread that is running your code. Exception in thread "main"中的异常意味着该异常由main()方法抛出,该方法也恰好是运行代码的主线程。

Method can throw exception class instance, isn't it?方法可以抛出异常类实例,不是吗?

Yes they can.是的他们可以。

But this is statement.但这是声明。 Why can statement throw exception class instance ?为什么语句可以抛出异常类实例?

Exception can occur anywhere.异常可能发生在任何地方。 So even statements can throw exceptions.所以即使是语句也可以抛出异常。 And in case you meant why can't statements throw exceptions,well they can.如果您的意思是为什么语句不能抛出异常,那么它们可以。 Here's an example:下面是一个例子:

if ((a/b) == 0) {
        throw new ArithmeticException();
    }

Second question is about thread "main".第二个问题是关于线程“main”。 What is thread "main" ?什么是线程“主”?

When a Java program starts up, one thread begins running immediately.当 Java 程序启动时,一个线程立即开始运行。 This is usually called the main thread of your program, because it is the one that is executed when your program begins.这通常称为程序的主线程,因为它是在程序开始时执行的线程。

Its Arithmetic exception case: means something in logic which not exist in Mathematical arithmetic;它的算术例外情况:表示逻辑中数学算术中不存在的东西

Check the condition of the "for loop" or any other logical functions.检查“for 循环”或任何其他逻辑函数的条件。

An example is when somewhere you write something like: (n % i == 0) and give initialization i = 0 , now every one knows that number can not be divided by Zero.一个例子是当你在某处写一些类似的东西: (n % i == 0)并给出初始化i = 0 ,现在每个人都知道这个数字不能被零整除。 So, you may need to change the initialization to i = 1 .因此,您可能需要将初始化更改为i = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“ main”中的异常java.lang.ArithmeticException:/减零-查找因素 - Exception in thread “main” java.lang.ArithmeticException: / by zero - finding factors 线程 awt-eventqueue-2 中的异常 java.lang.ArithmeticException / by Zero - Exception in thread awt-eventqueue-2 java.lang.ArithmeticException / by Zero Primefaces异常INFO:java.lang.ArithmeticException:/ by zero java.lang.ArithmeticException:/ by zero - Primefaces exception INFO: java.lang.ArithmeticException: / by zero java.lang.ArithmeticException: / by zero java.lang.ArithmeticException:/ by零 - java.lang.ArithmeticException: / by zero 线程“主”java.lang.ArithmeticException 中的异常:BigInteger 将溢出支持的范围 - Exception in thread "main" java.lang.ArithmeticException: BigInteger would overflow supported range 为什么我收到错误“线程中的异常”AWT-EventQueue-0“java.lang.ArithmeticException:/ by zero”? - Why am i getting the error “Exception in thread ”AWT-EventQueue-0“ java.lang.ArithmeticException: / by zero”? java.lang.ArithmeticException:除以零错误 - java.lang.ArithmeticException: divide by zero error 错误 java.lang.ArithmeticException:除以零 - Error java.lang.ArithmeticException: divide by zero java.lang.ArithmeticException:除以零 - java.lang.ArithmeticException: divide by zero java.lang.ArithmeticException:在displaytag中除以零 - java.lang.ArithmeticException: divide by zero in displaytag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM