views:

55

answers:

2

( patience requested as i'm new to programming )

when you add the phrase throws ABCexception 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

+1  A: 

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.

mjv
+1; except in case of `main()` there's no code that "called" this function - exception is simply printed out to stderr.
ChssPly76
@ChssPly76 Nxc6+ ;-) good move! I edited to explains this bubble-up logic. Thanks.
mjv
@ChssPly76: You are right. But technically, there is a native method that call main, it catch all `Throwable` and print them out (with stacktrace) to standard error. This is done transparently so it seems like there is an exception for `main()` (pun is not intended). :-D
NawaMan
+3  A: 

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.

NawaMan
"re-throw it up" - nice one :)
John Zwinck