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# Objects Inheritance Catching Exceptions

Matthew De-Veale Cutts
Matthew De-Veale Cutts
2,115 Points

What is "ex"? where did it come from? Why does it work?

I understand that Exception is from System, but what is this ex "keyword".

Kevin Gates
Kevin Gates
15,052 Points

Accidental comment, disregard.

1 Answer

Kevin Gates
Kevin Gates
15,052 Points

The 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

Kevin Gates
Kevin Gates
15,052 Points

Hi 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?

Matthew De-Veale Cutts
Matthew De-Veale Cutts
2,115 Points

Kevin Gates I think i get it! So Exception is a type (like int, double, etcetc) and ex is a variable name (like X)