SitePoint Sponsor

User Tag List

Results 1 to 25 of 117


Threaded View

  1. #14
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Halmstad, Sweden
    1 Post(s)
    0 Thread(s)
    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.
    Hey, you asked for it! 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:

    PHP Code:
    function GrowApples($apples) {
      if (
    $apples 1) {
    false//Relevant line
    } else {

    you do this:

    public void GrowApples(int apples) {
      if (apples < 1) {
        throw System.Exception(“Apples must be a positive integer!”);
      } else {
    Then, when using the method, you might do this:

    try {
    catch {
      Console.WriteLine(“Exception caught and handled!”);
    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.

    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:

    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 = "";
    		message.To = "";
    		message.Subject = "Unhandled exception!";
    		message.Body = strMessage;
    		SmtpMail.SmtpServer = "";
    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!
    Last edited by M. Johansson; Jan 21, 2003 at 11:45.
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts