Ah, okay, here goes. I’ll also explain exceptions, if you don’t know what it is. Whenever there is an error in your application while running, C# throws an exception. Now, if you don’t do anything with that exception, it will eventually end up on the users screen as an error. However, you can catch it, which is done like this:
try {
lowest = Convert.ToInt32(txtLowest.Text);
highest = Convert.ToInt32(txtHighest.Text);
}
catch (NullReferenceException e) {
// Stuff to do if the code inside the try-block produces an error.
}
Exception handling is one of the coolest things in ASP.NET, IMHO. If you don’t catch an exception, it will be thrown along up one level (i.e. if the error is produced inside a method, and not caught there, it will be thrown out of the method, and see if it’s catched there. It will actually be thrown all the way up to Application_Error in Global.asax. This is very nifty, since you can write an error handler there for all unhandled exceptions, and email them to yourself. Reeeally cool.
I’m familiar with the idea of using exceptions from playin around with try/catch in PHP5 but no real clue about exceptions themselves. I take it now I have that code I can head over to the .NET Documentation and look up some stuff I can do with NullReferenceException?
I’ll do that now and post back if I run into difficulties.
Thanks
Edit:
I got this error so I changed e to foo. Figured it was already declared somewhere and the easiest thing to do would be to change e to something else If I’ve done something wrong, let me know.
Compiler Error Message: CS0136: A local variable named ‘e’ cannot be declared in this scope because it would give a different meaning to ‘e’, which is already used in a ‘parent or current’ scope to denote something else
Lol, thanks for pointing that out. Nifty that only local users can see the details
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Line 10:
Line 11: try { Line 12: lowest = Convert.ToInt32(txtLowest.Text);
Line 13: highest = Convert.ToInt32(txtHighest.Text);
Line 14: } catch ( NullReferenceException foo ) {
Anyway, notice that it throws a FormatException and not a NullReferenceException, which I thought it would. The solution is to either make the catch block catch FormatExceptions:
catch (FormatException fe) {
or to make it catch all exceptions:
catch (Exception foo) {
You can also have multiple catch statements for different exceptions, if you like.
Also just a quick question, since I installed the .NET Framework and admittedly had a play with Directory Security in IIS, now normal ASP pages (ie. localstart.asp) won’t work. Any ideas for that? I don’t really have a use for old-ASP but if something is there I like it to work.
Thanks again for helping with the exceptions :tup: