![]() |
|
#1
|
||||
|
||||
C#... Data InputHello,
i didn't see a Visual C# thread here, but i thought maybe someone could help me anyway... this is for an into class, and my question is how to handle expections in C#. for example, my first program is supposed to find an average of a number of integers entered by the user. so when the user enters an integer everything is ok, but if a letter is entered it gives me an unhaldeled expection error. this is how i get the input from the textbox. C-SHARP / C# Code:
please hlep. __________________
Xrum! |
||||
|
#2
|
|||
|
|||
|
First about the forum. You are right in that there is no gidforum designated just for C#. However the most applicable forum for your questions considering C# would be .NET Forum - GIDForums. In the introductory sticky message of that forum it is explained that the Microsofts "managed code" concept including languages C#, Visual Basic .NET and JScript .NET are part of .NET framework.
Since the C# is "Microsofts Java", I would guess that error handling is managed by "try{...} catch(...){...}" structure. |
|
#3
|
||||
|
||||
|
Sorry, I couldn't resist...
We already have a working syntax highlighter just for C# on the new (test) forum. We hope to attract a bit more interest for the language once we upgrade the forums by the end of the month. In the meantime, I will move this discussion to the .NET forum. |
|
#4
|
||||
|
||||
|
Quote:
As maprich noted, you can make use of the try - catch mechanism. If you would have looked at the "Details" bit of the error message, you would have noticed that a System.FormatException was thrown. So catch this particular Exception. C-SHARP / C# Code:
Note you always have the possibility to catch a generic Exception - named just like that. This will catch *all* exceptions, because every exception class is inherited from this generic class. C-SHARP / C# Code:
Catching like that is not advised, except maybe in the development phase; you should catch and treat every exception your program is likely to throw. You can also display a much friendlier and comprehensive message, like I did in my little example. For completeness, I will add there is the possibility of using the final keyword too. The code in the final group is guaranteed to execute "no matter what" happens after the exception is caught. You will usually put clean-up code in here - but un-like in C++, there's not much clean-up you can do C-SHARP / C# Code:
Oh, and I advise against capitalization of the variable names; you will usually use a variable name of var and Var will be the accesor property for that variable (if any). But that's just the coding style I use, you are free to choose your own. Best regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
Recent GIDBlog
Vista ?Widgets? on Windows XP by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 13:12 |
| Mrs | stacy12 | C Programming Language | 14 | 05-Feb-2005 18:02 |
| [CONTEST?]Data Structure Test | dsmith | C Programming Language | 2 | 06-Jun-2004 15:13 |
| Script needed for letting user input a few days of data for tracking and analysis. | tradertt | MySQL / PHP Forum | 3 | 06-Mar-2003 02:54 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The