Ralt
Ralt

Reputation: 2134

Being specific with Try / Catch

Being new to programming I have only just found out that you can specifically catch certain types of errors and tie code to only that type of error.

I've been researching into the subject and I don't quite understand the syntax e.g.

catch (InvalidCastException e) 
 {
 }

I understand the InvalidCastException is the type of error being handled, however I am unsure of what e is.

Could somebody please explain this?

Upvotes: 11

Views: 543

Answers (10)

torrential coding
torrential coding

Reputation: 1765

e is the reference to the exception object that stores the details of the exception that was caught. The details may include an exception Message, an InnerException object, etc.

Upvotes: 3

Michael Bray
Michael Bray

Reputation: 15265

The e is the object that holds the data specific to the exception. If you look into different types of exceptions, you'll see that they all have different type of data. Many don't, but many do, and when they do, they can help you identify just exactly what happened instead of just getting a generic error.

For example, the NotFiniteNumberException defines an additional property called OffendingNumber that isn't present in a normal Exception object... This then provides additional data that you might need to figure out exactly what happened.

Upvotes: 16

user1252446
user1252446

Reputation:

"InvalidCastException" is the type, e is the object of this type. with the object, you can invoke member functions on it. for example

catch (InvalidCastException e) {
    ...
    e.printErrorMessage();
    ... 
}

Upvotes: 4

Dave Clausen
Dave Clausen

Reputation: 1318

e is the instance of the InvalidCastException type. In the catch block, you can for example say Debug.Write(e.ToString()).

Upvotes: 2

Mike Wade
Mike Wade

Reputation: 1746

e is the variable that the instance of InvalidCastException is placed into. You can then view the properties of that object to help you understand exactly what caused the error to be thrown by the code in the try block.

Upvotes: 4

usr
usr

Reputation: 171178

Suppose there was no e. How would you obtain the message of the exception?

The name e (or any other name) is there for you to get a handle on the exception object so that you can extract information from it.

It is legal syntax not to give out any name:

catch (InvalidCastException) //legal C#

This works, but you can't know anything else about the error except its type.

Upvotes: 20

Forty-Two
Forty-Two

Reputation: 7605

e is the variable that holds the exception so now you can do things like e.message and such

Upvotes: 2

Thousand
Thousand

Reputation: 6638

"e" is just whatever you decide to call the exception. just like you would do

string name = "...";

or

int total = 8;

where string and int in this example are the datatypes, (in your case the datatype is InvalidCastException) while "name" and "total" are the variable names, which in your case is just "e".

Upvotes: 6

Simon Martin
Simon Martin

Reputation: 4231

The 'e' is just the variable name, you could call it 'ex' or 'exception' if you find that easier to read. But 'e' is just a convention, like 'i' in a for i = 0... construct

Upvotes: 5

CassOnMars
CassOnMars

Reputation: 6181

e is a variable name. It can be whatever you'd like to name it, such as exception.

Upvotes: 2

Related Questions