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 trialsmatt
2,115 PointsWhat is "ex"? where did it come from? Why does it work?
I understand that Exception is from System, but what is this ex "keyword".
1 Answer
Kevin Gates
15,053 PointsThe idea is that if an error occurs, the system will return to you an exception object. Inside of your Catch function, you need to give this a name.
So the Exception ex
is the type then the name of that variable. It could have been for another function something like string MyString
.
So you're saying any parameter sent to my catch method of the type Exception
I would reference it as ex
.
Then you can later drill down into that object to find other things, like the exception's message.
You can read more about it here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch
smatt
2,115 PointsBut why is it "ex" is that syntax?
Kevin Gates
15,053 PointsHi Matthew De-Veale Cutts ,
the ex
specifically is the name you chose to give the variable.
So it's setup in a TYPE Name
relationship. So the TYPE is an Exception
and the Name is the name of the variable. This example chose ex
but it could be exception
or even random like BadNews
.
Make sense?
smatt
2,115 PointsKevin Gates I think i get it! So Exception is a type (like int, double, etcetc) and ex is a variable name (like X)
Kevin Gates
15,053 PointsCorrect.
Kevin Gates
15,053 PointsKevin Gates
15,053 PointsAccidental comment, disregard.