what is the difference between the following two codes? one of them is working fine while other is giving a compilation error.
compilation problem in code 1
class XYZ
{
public static void main(String[] args)
{
Throwable obj = new ArithmeticException("this is an error");
throw obj;
}
}
ERROR -
XYZ.java:7: error: unreported exception Throwable; must be caught or declared to be thrown
throw obj;
^
1 error
error: compilation failed
Successful execution in code 2
class XYZ
{
public static void main(String[] args) //throws InterruptedException
{
throw new ArithmeticException("this is an error");
}
}
output
Exception in thread "main" java.lang.ArithmeticException: this is an error
at XYZ.main(XYZ.java:6)
As we can see both the codes are identical but the one at the top is giving a compilation error whereas the second code is working without a flaw.
I wanna know
why this is so?
How can I make my first code work fine?