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 trialAdam Sawicki
15,967 PointsVariable in catch block
Hey,
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
2 Answers
naga pavan
558 Pointsexception 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.
manav
5,466 Pointscatch(<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) {
System.out.println(ex.getMessage());
}
Simon Coates
28,694 PointsSimon Coates
28,694 Pointscatch( <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.