您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)


Content Index

Content Page # 42

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

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg