Declaring exceptions in Java:
When declaring an exception in the method header, you use the keyword throws followed by the type of exception expected. We have seen this earlier. A further example is shown below, for a method that might be used to check that a particular data file had the expected format:
public boolean checkFormat (String fileName)
throws EOFException
{
// code for method checkFormat
}
If the method unexpectedly came to the end of the file when expecting to read more data, then the method would generate an exception of type EOFException.
The keyword throws advertises that the method is capable of throwing an exception. In any code that uses this method some action will need to be taken to cater for this exception.
A method may be capable of generating more than one type of checked exception. In this case you need to list all the possible types, for example like this:
public boolean checkFormat (String fileName)
throws EOFException, MalformedURLException
{
// code for method checkFormat
}
If some or all of the possible exception types in a list are subclasses of a particular exception class, it may be possible to shorten the list. For example, instead of the above, we could write:
public boolean checkFormat (String fileName) throws IOException
{
// code for method checkFormat
}
This works because EOFException and MalformedURLException are subclasses of IOException.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Declaring exceptions in Java homework and assignments? Live Declaring exceptions in Java experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Declaring exceptions in Java homework help, java assignment help and Declaring exceptions in Java projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours