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

Pranjal Agnihotri
Pranjal Agnihotri
4,187 Points

Exception Handling in Java

Why do we use "new" keyword while throwing exception. Like for e.g. throw new IllegalArgumentException. Why did we use new there.

1 Answer

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hi Pranjal Agnihotri !

Because it is a class, so you are creating a new instance of the IllegalArgumentException class.

If you are really interested you can take a look at the source code since everything is open.

Does that make sense?

Pranjal Agnihotri
Pranjal Agnihotri
4,187 Points

Thank you sir , Now I understood that's why you were using getMessage() which is a method of that IllegalArgumentException class.