开发者

does throws exception leads the program to a pre written code

( patience requested as i'm new to programming )

when you add the phrase throws ABC开发者_开发知识库exception in method declaration like this

public static void main(String[] args) throws ABCException {

}

does it mean that the you expect the method could generate an ABC exception and by writing throws ABCException ... when this exception occurs .. this exception will be caught and some prewritten code in java language will be executed corresponding to the ABCException. ?

thanks


throws declares that the method may throw the exception so the code that invoke this method must be prepare for it.

Being prepared for exception means that the code may catch the exception or re-throw it up.

Consider the constructor new FileInputStream(File pFile) which will create a FileInputStream from a File object. Since the file may not exist or not readable the constructor will throw FileNotFoundException (as declared).

So any code that call this constructor will either catch or rethrow it.

Catching it, the code will taking care of that exception by themself while rethrowing it, the code will let its caller take care of it.

Consider the following two codes:


Code 1: Catch -> Take care of it

public String readTextFile(File pFile) {
    try {
        FileInputStream FIO = new FileInputStream(pFile);
        ... // Do the reading and return
    } catch (FileNotFoundException E) {
        System.err.println("The file is not found");
    }
}

Code 2: Throw -> Let the caller take care of ot

public String readTextFile(File pFile) throws FileNotFoundException {
    FileInputStream FIO = new FileInputStream(pFile);
    ... // Do the reading and return
}

So the caller of readTextFile will have to catch or rethrow it too.


This mechanism ensures that someone have to take care of the exception in someway.

I hope I help.


The thows ABCException statement, before the function definition starts in earnest is just to indicate that this function may throw such an exception.

The actual throwing of the exception would happen within the code of the function. With code like the following (note the lack of an 's' at the end of "throw")

  throw new ABCException();

The exceptions are then passed on "up" through the chain of the program logic that called this function this function, until one these "catches" the exception and deals with it. In case the exception "bubbles back" all the to the main() function, and if said exception isn't caught there either, a default handler deals with it, typically by printing out the exception to stderr/stdout and halting.

The way this exception could be caught would be with a try-catch construct, as in:

try
{
  // do some stuff if needed
  xy = fct();  // this fct may throw the ABCExeption...
  // do more stuff as well
}
catch (ABCException e)
{
  // for debugging you can do this
  e.printStackTrace();
  // otherwise you could deal with this exception as desired.
}

In re-reading the question, I noted that the function which is declared with throws ABCException and that is a bit odd, because main is the first method in the chain of function calls, meaning that there is nothing before main() which could catch an exception (other than the default exception handler of the Java Runtime, which wouldn't do anything specific for ABCException; it would just "dump it" to the console (or elsewhere for GUI-based apps) like any other exception.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜