"CSS Layout Basics" was retired on July 12, 2021. You are now viewing the recommended replacement.

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

C# C# Basics (Retired) Perfect "try" Code and "catch" Exceptions

Feeling Overwhelmed and Frustrated by Try/Catch Concept (Since It's Used Heavily in the Final Code Challenge)

After watching and re-watching Jeremy's video on Try/Catch & after seeing its importance in the section's final code challenge, instead of understanding the concept during his about 1 min explanation of Try/Catch I feel completely overwhelmed and confused about how to even approach this concept.

I image that Try/Catch is like being stuck on an island. A person rows up and says, "I'm not going to save you, but I'll tell you that you can survive on this island: you need to get the Quaintamaplom"...and then rows off. I'm left with no clues, no tools, and no idea what a Quaintamaplom is, how to catch it, or what it is...is it a tool? Something to eat? Is it a magical transporter? Who knows! If I can make a searching and trapping device for this Quaintamaplom, how diverse does it need to be to catch the Quaintamaplom?

In my island example catching a Quaintamaplom is hopeless because I have too little information and knowledge to work with to know A) what a Quaintamaplom is B) how to find one C) how to catch one D) how to find information on how to reliably catch one in the future so I can survive.

I feel this way entirely about Try/Catch. The video never discussed what Catch even is, but my understanding is it's exception (aka fatal(?) error) handling...but how do we know what exceptions to handle, especially as a beginner? (aka how wide to cast our trap to search for and catch the Quaintamaplom) How do we know what we're trying to catch (aka how to catch a Quaintamaplom) & that this exception is even a problem in the first place for our program. One of the following videos in the series suggests: oh simply check the .NET Framework page to see the exceptions, but that's assuming that I know what class/method I'm looking for, what the exceptions mean, if the exceptions are relevant to my program, and other things that a beginner can't possibly know.

Can someone help me clear this up in a way that can be easily understood (please don't point to Stack Overflow as that quickly goes over my head)? Thanks!

Nichelle Segar
Nichelle Segar
9,857 Points

Great analogy. LOL, I'm still trying to figure this out, too.

2 Answers

Christopher Debove
PLUS
Christopher Debove
Courses Plus Student 18,373 Points

Well simply said : Try/Catch is just => Try to execute this code (try block) and if an error is thrown while executing this part of the code then stop and execute catch block;

If you don't know which error can be thrown, you can write a catch without parameter.

try
{
  // Anything you want
}
catch
{
  // Oh an error has been thrown handle it
}

Hi

I think you may be taking the concept unnecessarily too deep. Let back up a bit with a simple example

Let say you have a simple code that does this (pseudo code)

int a, b, c;

double e;

a=1; b=2; c=0

e = (a+b) / c; /// oops I forgot to set c to something other than zero

If you run the program like this, the program will crash and the end user will get a long cryptic message from the compiler.

now if I do this

try{

a=1; b=2; c=0

e = (a+b) / c; /// oops I forgot to set c to something other than zero

print("Hello ... c=,%s" , c);

exit; } catch(Exception e){

print ("wow there ... you are dividing by zero ... call the developer and have him check the code") exit; }

with try /catch, when the code execute e = (a+b) / c; an exception will be thrown to the catch block (instead of sending up the compiler call stack to crash the program) with nasty cryptic compiler message printed on the screen.

Once in the the catch block, a nicer message will be displayed and the program will exit gracefully.

Now image this situation, your code is getting data from the data base ... and someone put bad data in the data base ... if you don't put try catch ... you will have hard time figuring out what happened and where di it happen ... plus you may make few end users unhappy.

Hopefully this answers your question, if so, please mark the question as answered.