This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
It's created a new Exception at line 3. But it hasn't been thrown. There's no throw new Exception... statement. And unless an Exception is thrown it's just like any other object.
It would be very unusual to use it like this. But a common occurrence would be to catch an exception and then pass it into a method (e.g. a logging method) - that's not really all that different to what's happening here.
Note that since it's an Exception, not a RuntimeException, if it was being thrown the compiler would force you to handle or declare it.
Are both checked and unchecked are checked by compiler at compile time ?
Only checked exceptions are checked by the compiler during compile time . The unchecked exceptions are not checked by the compiler . So , this is also a classification of exceptions : 1>checked
If you change this code like the above, the main method does throw the checked exception at run time. You can declare a Runtime exception too.
In order to have the method throw an exception, put "throw new XXException()" in a line, and declare it in the method. This will make the method throw the exception.