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 trialKevin Gonzales
4,638 PointsHelp with Throws and catches.
I am having difficulty understanding why we use throw and catch.If someone could give me a detailed, but simplistic explanation as tho why use it and when ect It would be greatly appreciated.
1 Answer
Lauren Kimlinger
2,470 PointsThrowing exceptions and catching them is a way to deal with the unexpected failure of your code. Usually you would use them when asking for user input. Users are a major source of unexpected failures.
It is possible to use "if" statements to deal with the same situations, but if you forget about or fail to anticipate a certain way your code can fail, it will fail and possibly take the computer down as well. Using exceptions is like an insurance policy.
Also, exceptions are built into the language, so other programmers and their code will know what to do when your code throws an exception.
Kevin Gonzales
4,638 PointsKevin Gonzales
4,638 Pointsoh so its sort of like an if else statement, but meant for use for user input?
Lauren Kimlinger
2,470 PointsLauren Kimlinger
2,470 PointsI mentioned user input as a common use of try...catch blocks, but you could (and should) also use them when opening files, trying to retrieve information from the internet, or iterating over a list, among others. Any code that calls on an unreliable outside source should be expected to fail, sometimes in ways you didn't expect. They are like if...else statements in structure, where you enumerate different conditions and what to do in each case (this is the "catch" part of the statement).
I'm looking back at your original question, and it is about throws and catches. I was describing a try...catch block. Throws are usually used inside "if" statements when the programmer wishes to throw a specific exception in response to a particular failure of the code. Throws generally are a way to inform the user or the calling code that something has failed in the code you have written.
Kevin Gonzales
4,638 PointsKevin Gonzales
4,638 Pointsok I need to look more into how to implement the try catch block (videos didnt help me understand too much on that part), but I understand now that they are used when getting data from unreliable sources (online). So thank you. If you have any links ect to where I may learn how to use the try catch it would be helpful for me. Regardless Thank you.
Lauren Kimlinger
2,470 PointsLauren Kimlinger
2,470 PointsGlad to help. Here's a link I found helpful: https://docs.oracle.com/javase/tutorial/essential/exceptions/advantages.html
Kevin Gonzales
4,638 PointsKevin Gonzales
4,638 PointsThank you very much!