function throw exception java code example

Example 1: throw io exception java

public static void foo() throws IOException {
    // some code here, when something goes wrong, you might do:
    throw new IOException("error message");
}

public static void main(String[] args) {
    try {
        foo();
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

Example 2: throw and throws keyword in java

Throws keyword used for handling exceptions. 
  Where do you use it? Methods signature. 
 If you want to handling right away in selenium or Api use “throwskeyword.
Throw is creating an exception. Basically there are doing opposite. 
Where do you use it? We use it with in the block.

Tags:

Java Example