|
|
Throwing an exception The previous section explained how to define a new exception, called NotTextFileException. This exception was to be thrown by the method openTextFile() if the user tried to open a file that was not a text file. Here is how the exception is thrown: throw (new NotTextFileException()); Note that it is a specific object of the class that is thrown, not the class itself. So we use new to create the exception object, and throw to throw the new exception. A program can throw any exception, not just the ones it defines. However, this is usually not useful; the built-in exceptions are thrown by the methods in the built-in classes. Back to top 
RITSEC - Global Campus Copyright ?1999 RITSEC- Middlesex University. All rights reserved. webmaster@globalcampus.com.eg |
|