以下陈述java.lang.ArithmeticException: / by zero显而易见.
System.out.println(0/0);
Run Code Online (Sandbox Code Playgroud)
因为文字0被认为是int文字并且在整数算术中不允许除以零.
但是,以下情况不会抛出任何异常java.lang.ArithmeticException: / by zero.
int a = 0;
double b = 6.199;
System.out.println((b/a));
Run Code Online (Sandbox Code Playgroud)
它显示Infinity.
以下语句生成NaN(非数字),没有例外.
System.out.println(0D/0); //or 0.0/0, or 0.0/0.0 or 0/0.0 - floating point arithmetic.
Run Code Online (Sandbox Code Playgroud)
在这种情况下,两个操作数都被认为是双精度数.
同样,以下语句不会抛出任何异常.
double div1 = 0D/0; //or 0D/0D
double div2 = 0/0D; //or 0D/0D
System.out.printf("div1 = %s : div2 = %s%n", div1, div2);
System.out.printf("div1 == div2 : %b%n", div1 == div2);
System.out.printf("div1 …Run Code Online (Sandbox Code Playgroud) 有什么情况throw可以避免可以避免的错误吗?
我正在考虑DivideByZeroException和ArgumentNullException
例如:
double numerator = 10;
double denominator = getDenominator();
if( denominator == 0 ){
throw new DivideByZeroException("You can't divide by Zero!");
}
Run Code Online (Sandbox Code Playgroud)
有没有理由抛出这样的错误?
注意:我不是在谈论捕捉这些错误,而是专门知道是否有充分理由抛弃这些错误.
只是重新评估:
我知道在我给你的例子中你可能会更好地处理错误.也许这个问题应该改写一下.是否有任何原因导致throw其中一个错误,而不是在此位置处理它.
c# exception-handling argumentnullexception dividebyzeroexception
我在VS2015 C#interactive中运行了以下代码片段并得到了一些非常奇怪的行为.
> double divide(double a, double b)
. {
. try
. {
. return a / b;
. }
. catch (DivideByZeroException exception)
. {
. throw new ArgumentException("Argument b must be non zero.", exception);
. }
. }
> divide(3,0)
Infinity
> 3 / 0
(1,1): error CS0020: Division by constant zero
> var b = 0;
> 3 / b
Attempted to divide by zero.
>
Run Code Online (Sandbox Code Playgroud)
为什么该方法返回无穷大而3/0引发错误并且3/b抛出格式化错误?我可以强制分裂抛出错误而不是返回无穷大吗?
如果我重新格式化方法
double divide(double a, double b)
{
if ( …Run Code Online (Sandbox Code Playgroud) 通过NUnit运行以下C#代码
Test.ControllerTest.TestSanity: Expected: `<System.DivideByZeroException>` But was: null
Run Code Online (Sandbox Code Playgroud)
因此要么不抛出DivideByZeroException,要么NUnit不捕获它.与这个问题类似,但他得到的答案,似乎并不适合我.这是使用NUnit 2.5.5.10112和.NET 4.0.30319.
[Test]
public void TestSanity()
{
Assert.Throws<DivideByZeroException>(new TestDelegate(() => DivideByZero()));
}
private void DivideByZero()
{
// Parse "0" to make sure to get an error at run time, not compile time.
var a = (1 / Double.Parse("0"));
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
根据http://msdn.microsoft.com/en-us/library/system.dividebyzeroexception.aspx, 只有Int和Decimal在将它们除以0时会抛出DivideByZeroException,但是当你将浮点除以0时,结果是无穷大,负无穷大或NaN.为什么是这样?结果是+ ve无穷大,-ve无穷大还是NaN的例子是什么?
以下代码的输出让我感到困惑.为什么NaN有时和无限其他时间?
public static void main (String[] args) {
double a = 0.0;
double b = 1.0;
int c = 0;
System.out.println(a/0.0);
System.out.println(a/0);
System.out.println(b/0.0);
System.out.println(b/0);
System.out.println(c/0.0);
System.out.println(c/0);
}
Run Code Online (Sandbox Code Playgroud)
输出是:
NaN
NaN
Infinity
Infinity
NaN
Exception in thread "main" java.lang.ArithmeticException: / by zero
Run Code Online (Sandbox Code Playgroud)
这里的决定因素是什么?
以下是我认为任何人都需要能够评估我的问题的所有代码
1 import.java.util.Scanner
2 public class ccattano_Sieve{
3 private boolean [] primes = new boolean [50001];
4 private int upper;
5 private int lower;
6
7 public ccattano_Sieve(){
8 upper = 50000;
9 lower = 1;
10 for (int i = 2; i < primes.length; i++){
11 primes[i] = true;
12 }
13 primes[0] = false;
14 primes[1] = false;
15 }
16
17 public void processSieve(){
18 for (int i = 2; i < Math.round(Math.sqrt(50000)); i++){
19 if (primes[i] …Run Code Online (Sandbox Code Playgroud) 我正在开发一个非常小的程序来查找C++中整数的除数.我的main方法几乎将int转换为var,并使用int作为参数调用factor方法.这是代码:
void factor(int num)
{
for(int x = 0; x < ((num + 2) / 2); x++)
{
if((num % x) == 0)
{
cout << x << " ";
}
}
}
Run Code Online (Sandbox Code Playgroud)
程序总是在factor()内崩溃.如果我使用此代码,它运行正常:
void factor(int num)
{
for(int x = 0; x < ((num + 2) / 2); x++)
{
{
cout << x << " ";
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以问题出在了if((num % x) == 0).当我将该行更改为if((num % 2) == 0)或时if((num % 5) == 0),它会产生正确的结果(我使用32作为测试输入). …