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!

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 Objects (Retired) Harnessing the Power of Objects Exceptions

Adam Sawicki
Adam Sawicki
15,967 Points

Variable in catch block


Would you explain to me one thing about try-catch block? The catch block looks like : catch( <Exception name> <variable name>), and here is my question: What is stored in variable and what can i use it for?

thanks in advance

Simon Coates
Simon Coates
28,694 Points

catch( <Exception name> <variable name>) the exception name is the type of exception, you're expecting, while the variable name is the name for the specific instance of that exception class. The aim is to either use the information about the exception to recover from the problem or give some message to the user.

To see the methods available, you can view the documentation for the particular exception class.

2 Answers

exception name is a pre-built class in java and to access those methods we create objects by keeping a variable name. This variable name has the access to all the methods in the exception class.

catch(<Exception name> <variable name>)

What is stored in variable? - An object or instance of class <Exception name>

What can I use it for? - To access methods like getMessage()

try {
} catch (IllegalArgumentException ex) {