Hey, you asked for it!Originally posted by firepages
I have only just started playing with C# but please enlighten , what raw power are we talking about , I also try my best in C++ which is definately more powerful than PHP in many respects but not necc'ly the best bet for web-applications, hence .php and not .dll/so
please show why (as a web-application probramming language) it is so bad compared to C# preferably without note of 'everything is an object' the merits of which are subjective, nor PHP's current OOP which whilst more than usable will probably be updated around the same time as .NET gets its own server give or take a few months.I’ve actually been waiting for an excuse to write a little piece in C# vs. “PHP – The language” for a while now. I give you:
Nice stuff C# has - Part #1
Exception handling
In PHP, a function normally just returns “false” when it bombs. In C#, you don’t have to do this, instead, you throw an exception. Instead of:
you do this:PHP Code:function GrowApples($apples) {
if ($apples < 1) {
return false; //Relevant line
} else {
$applesInTree++;
}
}
Then, when using the method, you might do this:Code:public void GrowApples(int apples) { if (apples < 1) { throw System.Exception(“Apples must be a positive integer!”); } else { applesInTree++; } }
Understand what happens? The “try” block is what gets executed first. Now, IF that code throws an exception (by using the throw statement mentioned above), the “catch” block executes, INSTEAD of spitting out the raw error to the user. Now, this is very smooth, since if you don’t handle this particular exception with a catch block, the program will unwind the call stack, and back up the code in levels until it finds a catch statement.Code:try { GrowApples(5); } catch { Console.WriteLine(“Exception caught and handled!”); }
In plain English, this mean that you can have an ASP.NET page that calls one method, which in turn calls another method, which does some calculation and then calls another method and THAT one causes an exception, the program will back up through your code until it finds a catch statement that will take it. So you might have a catch-block at the top of your app, which catches the error and takes the message of it and assigns to a web forms label, or perhaps redirect to different pages depending on what error was produces.
This can produce extremely elegant and easy-to-implement error handling, especially in the case of ASP.NET applications where you can define an Applicaton_Error event in global.asax that picks up EVERY exception in the application that isn’t handled by a catch block:
Now, the Server.GetLastError().InnerException property is actually a reference to the exception that caused the latest error. This contains various properties, such as the Error Message, for instance, but what is really interesting here is the Stack Trace which gives us a detailed list of what methods were called in what order on what line, which gives us the exact path the program took before it bombed. Invaluable for debugging, I tell ya!Code:protected void Application_Error(Object sender, EventArgs e) { if (Global.MailErrors) { //Get error Details string strMessage = "\n\nURL:\n " + Request.Path + "\n\nMESSAGE:\n " + Server.GetLastError().InnerException.Message + "\n\nSTACK TRACE:\n" + Server.GetLastError().InnerException.StackTrace; MailMessage message = new MailMessage(); message.From = "server@mydomain.com"; message.To = "Admin@mydomain.com"; message.Subject = "Unhandled exception!"; message.Body = strMessage; SmtpMail.SmtpServer = "smtpserver.mydomain.com"; SmtpMail.Send(message); } }



I’ve actually been waiting for an excuse to write a little piece in C# vs. “PHP – The language” for a while now. I give you:









Bookmarks