Reputation: 255
This piece of code shows exception:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Ankit2.main(Ankit2.java:6)
Why and how its happening? Without using try and catch block?
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);
}
}
Upvotes: 2
Views: 192
Reputation: 132
Operations like dividing by zero throw an unchecked exception. This is why your code compiled just fine without a try/catch block. That does not mean that no exception will be thrown at runtime. Look up the difference between checked and unchecked exceptions.
Upvotes: 4
Reputation: 262464
That is a RuntimeException.
You do not have to declare or catch those.
Any exception that your code does not catch (declared or not), will crash the running thread. In the case of the main thread that started the program, the JVM will print the stacktrace before exiting.
Upvotes: 7