Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
PLUS
Dennis Addo
Courses Plus 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