如果我试图打印"a"的值,为什么显示错误?为什么异常会成为错误?
class Ankit1
{
public static void main(String args[])
{
float d,a;
try
{
d=0;
a=44/d;
System.out.print("It's not gonna print: "+a); // if exception doesn't occur then it will print and it will go on to the catch block
}
catch (ArithmeticException e)
{
System.out.println("a:" + a); // why is this an error??
}
}
}
Run Code Online (Sandbox Code Playgroud) 这段代码显示异常:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Ankit2.main(Ankit2.java:6)
Run Code Online (Sandbox Code Playgroud)
它为什么以及如何发生?不使用try和catch块?
class ankit1
{
public static void main(String args[])
{
float a=20,b=120,c=50,sum;
sum=(a+b+c)/0;
System.out.println("The average of three number is:"+sum);
}
}
Run Code Online (Sandbox Code Playgroud)