0

I saw a video that explains throws and throw as follows:

Throws - is used to delegate/pass on the exception to the caller method.

class test{

    void child() throws filenotfoundException{ 

    //////## this method passes the exception to its caller which is main method 
        File f = new File("abc")
    }

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

    //////##this main method passes the exception to its caller which is JVM 

    }
}

So in the above example, if file 'abc' doesn't exist, and the exception is not handled using try catch in the child method, the child method will pass on the exception to main method.

As the main method doesn't handle the exceptions using try catch, it also throws or passes the exception to its caller method which is JVM.

Is it correct?

Throw - JVM only understands the logic to pick up predefined exceptions. And hence all the user defined exceptions should manually be created using new Exception and then passed on to JVM using throw keyword.

is it right as well?

lynxx
  • 544
  • 3
  • 18
  • 1
    There is no much to expalain as `throws` is a part of method declaration and `throw` is instruction keyword to "throw" given exception. – Antoniossss Mar 27 '19 at 19:48
  • 2
    A method which `throws` might throw that exception. `throw` actually throws an exception. – Andy Turner Mar 27 '19 at 19:49
  • 1
    Your example is wrong. A `java.io.File` object doesn't represent an actual file, only a filename; `new File("name")` never throws `FileNotFoundException` (which is camelcase not lowercase). A method or ctor that tries to access the actual file such as `new FileReader (new File ("nonexistent"))` does throw. – dave_thompson_085 Mar 28 '19 at 16:43

2 Answers2

0

throws is used in the method signature to tell everyone that "yes, this method throws an Exception of X type, please be wary" while throw is used to actually launch this Exception and abort the execution of the function.

Yes, depending on the exception, and if you catch it or not, it might bubble all the way up to the JVM. Some Exceptions do not cause the execution to abort however. These are known as RuntimeExceptions. The programmer can decide to handle them, or not, using a try/catch block. RuntimeExceptions do not need to be declared in the function signature.

Ferdz
  • 1,182
  • 1
  • 13
  • 31
0

The explanation is flawed. First, child() as written isn't going to throw a thing. File not found is not an exception so far and wouldn't be unless you tried to open the file for input.

From your code, though, child() could possibly throw the exception, as could main(). Child doesn't, but it's defined that it might.

Throw is how you actually, well, throw the exception. Like this:

void child() throws FileNotFoundException {
   File f("garbage");
   if (!f.exists()) {
       throw new FileNotFoundException("the world is clean as garbage does not exist");
   }
}

Your main() doesn't have a try/catch block, but it could:

void main() {
   try {
       child();
   }
   catch (FileNotFoundException e) {
       // Oh no!
   }
}
Joseph Larson
  • 8,530
  • 1
  • 19
  • 36