I have two questions about 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;
A method can throw an Exception
class instance, can't it? But this is an expression. Why can an expression throw an Exception
class instance ?
My second question is about thread "main"
. What is thread "main"
? Does "main"
mean the static main
method?
java.lang.ArithmeticException
is a type ofException
that aims to better describe what the problem actually is. There's no point just creating anException
, as it really could be caused by anything. By generating anArithmeticException
, the user can immediately know that the problem is something to do with a calculation. AnException
can be thrown by any piece of code, including in calculations such as your example.Exception in thread "main"
means that the exception is thrown by themain()
method, which also happens to be the primary Thread that is running your code.