This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Basically, a method can throw an exception when something wrong happens. That's what the "throw" keyword is for. You already know sun's tutorial, so you are already familiar with checked and unchecked exceptions. If you throw a checked exception from a method, you have to declare it, that's what the "throws" keyword is for. When you want to call this method, it tells you that this method throws TestException and that it should be delt with.
In the above example, the name must not be null. You check that name is not null. If it is null, you throw a TestException. If TestException is a checked exception, you have to declare that the sayHello method throws it. So you declare your method as "public String sayHello(String name) throws TestException".
The code calling this method will have to deal with this exception. So you should have something like :
Joined: Sep 12, 2008
When i run the above code no doubt i get the output as :
But when i modify as :
since i left the string intentionally blank i should get an error.