Adblock breaks this site

[TUT][C#]The Try/Catch/Finally statment.

Discussion in 'Programming General' started by Pur3 Bull3t, Jun 7, 2009.

Thread Status:
Not open for further replies.
  1. Pur3 Bull3t

    Pur3 Bull3t Member
    Banned

    Joined:
    May 29, 2009
    Posts:
    46
    Referrals:
    0
    Sythe Gold:
    0
    [TUT][C#]The Try/Catch/Finally statment.

    I've noticed many people do not know how to use try/catch/finally correctly..

    In short, the try statement will execute the lines within it, if it cannot succeed it moves to the catch, and finally will ALWAYS execute.

    I've designed a simple ping / portscan application to demonstrate.

    Code:
    System.Net.Sockets.TcpClient tcp = new System.Net.Sockets.TcpClient();
    
    string ip = "127.0.01";
    int startPort = 1
    int endPort = 100
    
    for (int x = startPort; x <= endPort; x++;)
    {
           try
               {
                   tcp.Connect(ip, x);
                   Console.WriteLine("Port: " + x.toString() + " is open.'');
                }
            catch
                {
                   Console.WriteLine("Port: " + x.ToString() + " is closed...");
                 }
             finally
                 {
                    Console.ReadLine();
                  }
    }
    
    
    
    
    Sorry for the sloppiness, I wrote this in the post itself.

    I hope people have learned the functionality of Try/Catch/Finally Statements.
     
  2. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    [TUT][C#]The Try/Catch/Finally statment.

    This is hardly a tutorial ... It's like saying "Heres some code, now YOU figure out what it does."
     
  3. Pur3 Bull3t

    Pur3 Bull3t Member
    Banned

    Joined:
    May 29, 2009
    Posts:
    46
    Referrals:
    0
    Sythe Gold:
    0
    [TUT][C#]The Try/Catch/Finally statment.

    It's supposed to explain the try/catch/finally statements, not the code itself.
     
  4. crackmyknuckles

    crackmyknuckles Newcomer

    Joined:
    Jan 9, 2009
    Posts:
    15
    Referrals:
    0
    Sythe Gold:
    0
    [TUT][C#]The Try/Catch/Finally statment.

    Actually that example is only one of the many the correct usages of try/catch/finally blocks.

    An example being:
    Code:
    try
    {
    //Some code which may or may not throw a specific exception
    }
    catch(SpecificExecption e)
    {
    //do something that is specific to this error
    }
    catch(Exception e)
    {
    //do something for the non-specific errors.
    }
    finally
    {
    //release the objects in the try block or other clean up operations 
    }
    
    Sorry for bad formatting, in this example the code will catch specific errors allowing you to determine the exact reason the code "broke".

    I should also note that the finally block is not always needed. In my opinion, unless you are really starved for resources you can let the "Garbage Collector" free those resources unless it is a connection or a stream in which case the object should ALWAYS be closed and disposed of by hand.'

    Just my 2 Cents.
     
  5. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    [TUT][C#]The Try/Catch/Finally statment.

    You're misunderstanding - you're not even telling the person what it actually does, or how it functions.

    This isn't a tutorial.
     
  6. crackmyknuckles

    crackmyknuckles Newcomer

    Joined:
    Jan 9, 2009
    Posts:
    15
    Referrals:
    0
    Sythe Gold:
    0
    [TUT][C#]The Try/Catch/Finally statment.

    I agree, however so as to not lose a tut for a subject that seems to have few I will help.

    A try/catch/finally block is C#'s way of Error Handling. All Class's and their respective objects and methods all in some way use either the base Class Exception or an inheritance of it. Exception is the Class(and Object) in which all of the other classes will report errors. They will do so by "Throwing" Exceptions.

    A try/catch/finally block is used to run code and if an exception is thrown, suppress it in a way that the programmer decides (Such as sending it to a MessageBox Object). That way, the end user does not see the rather ugly way the .Net Framework handles them, which is a generic MessageBox with a lot of info the end-user does not need to see.

    They also give the program the ability to "break" without completely disabling the program. One part of the program can break, and instead of the program completely crashing, the exception is caught and handled, most times without the user knowing it.

    The complete break down if a try/catch/finally block is this:

    Code:
    try
    {
    //Code
    }
    Any code in this try block will execute until the end of the code is reached OR when a piece of code throws an exception. That exception will be "Caught" by the catch block:

    Code:
    catch(Exception e)
    {
    //Error Handling code
    }
    Now this catch block will catch any exception since all other exceptions inherit from the base Exception class referenced in the (). However if a specific exception such as:

    Code:
    catch(ArrayOutOfRangeExecption e)
    {
    //Specific Array error handling code here.
    }
    is written before the base Exception catch block, and that specific ArrayOutOfRangeException was thrown, that catch block will handle the exception and then will fall through to the finally block.

    The finally block is not always needed, however you should only not use it if when writing your code, you have nothing to put there. Like i said in my above post, I mainly only use it for persistent streams such as StreamWriter/Reader and Socket Transfers.

    As the Original Poster stated the code in the finally block WILL ALWAYS execute whether there was an Exception thrown or not. So be careful what you put in your finally blocks.

    Happy Programming.
     
  7. Pur3 Bull3t

    Pur3 Bull3t Member
    Banned

    Joined:
    May 29, 2009
    Posts:
    46
    Referrals:
    0
    Sythe Gold:
    0
    [TUT][C#]The Try/Catch/Finally statment.

    Yes, I do explain what it does...
    crackmyknuckles, just owned it and made it much better?
     
  8. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    [TUT][C#]The Try/Catch/Finally statment.

  9. Pur3 Bull3t

    Pur3 Bull3t Member
    Banned

    Joined:
    May 29, 2009
    Posts:
    46
    Referrals:
    0
    Sythe Gold:
    0
    [TUT][C#]The Try/Catch/Finally statment.

    Yes, that is explaining.
     
  10. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    [TUT][C#]The Try/Catch/Finally statment.

    *facepalms*

    That is like explaining how the physics of a bicycle works by saying "So you push on the pedals and it moves forward."

    It isn't much of an explanation, it doesn't help people understand how and why it works, nor where to use it in the first place. It doesn't help to learn anything, save maybe the goddam keywords which are completely useless unless you DO know the how and why behind it.

    Face your error; this is in no way a tutorial.
     
  11. Pur3 Bull3t

    Pur3 Bull3t Member
    Banned

    Joined:
    May 29, 2009
    Posts:
    46
    Referrals:
    0
    Sythe Gold:
    0
    [TUT][C#]The Try/Catch/Finally statment.

    Oh yes it is, just not a very good one.
    You're pointing out obvious flaws, nobody is impressed. ;)
     
  12. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    [TUT][C#]The Try/Catch/Finally statment.

    And nobody is impressed by your "tutorial" either. Feel free to rewrite it to actually teach something, but for now I'm closing your thread.

    Please note that a TUTORIAL is meant to TUTOR. What you posted was a brief outline.
     
< Need help creating server - need someone to explain the basic idea | Writing to Webserver[help] >
Thread Status:
Not open for further replies.


 
 
Adblock breaks this site