Java Java Data Structures Efficiency! Design the UI

Full explanation of the throw and throws keywords please.

Full explanation of the throw and throws keywords please.

3 Answers

Dennis Addo
PRO
Dennis Addo
Pro Student 2,943 Points

I think the Throws clause in used to declare an exception and throw keyword is used to throw an exception explicitly. If we see syntax wise than throw is followed by an instance variable and throws is followed by exception class names.

Throw:
    throw new Exception("You have some exception")
    throw new IOException("Connection failed!!")
Throws:

throws IOException, ArithmeticException, NullPointerException  ....... usw.
explicitly und declaration:

public int findMax(int[] array) throws Exception{
    if(array==null)
       throw new NullPointerException(...);
    ...}

Thank You