i intended to examine the usage of "throws ArithmeticException" in this tiny program, but with or without, it gives the same runtime exception, until i declare try block as shown above, the program runs fine.
May I ask how do we effectively implement throws clause?
boolean b = true;<br />System.out.println ("I believe in Java.<br />Java will make my dream come " + b);
ArithmeticException is a subclass of RuntimeException, so it's an unchecked exception. This means that it doesn't matter whether or not you explicitly declare it in your method's throws clause. The compiler will not require the calling code to handle the exception if you declare it, nor will it stop your code from throwing the exception if you don't declare it.
However, this isn't the case with checked exceptions. Normally you create your own exception class and then have your methods throw it to indicate an error condition. Here's a fairly typical example of how a real program might look like: However, the above program will not compile. Since NoSuchWidgetException is a checked exception, the getWidget() method will not compile unless you declare that it throws the exception. And once you add a throws clause with a checked exception, the compiler will insist that you either put every invocation within an appropriate try...catch statement, or change the calling method to declare it too throws the exception. For instance: