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

Akira  Noguchi picture Akira Noguchi · May 30, 2012 · Viewed 47.4k times · Source

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?

Answer

wattostudios picture wattostudios · May 30, 2012

java.lang.ArithmeticException is a type of Exception that aims to better describe what the problem actually is. There's no point just creating an Exception, as it really could be caused by anything. By generating an ArithmeticException, the user can immediately know that the problem is something to do with a calculation. An Exception 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 the main() method, which also happens to be the primary Thread that is running your code.