Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

C# Try and Catch [Intermediate Level Tutorial]

C# Try/Catch


What Is It:
Have you ever been running your application and all of a sudden something goes wrong? Your program crashes and you are left in a daze of confusion and sadness :(.

After setting up a try / catch statement things will work more smoothly. If your compiled application runs into an error, rather than crashing, it will display the error inside of a messageBox.

Getting Started:
So to begin "Trying" to execute code we simply do this:
Code
Try
{
   Code Here
}
Catch (System.Exception)
{
   MessageBox.Show("There was a super error!");
}


The First Line begins trying the code.
The last lines catch the error and display a custom messagebox!


If you want to display the actual error, rather than a custom message, do the following:

Code
{
    try
    {
    Code Here
    }
    catch (System.Exception error)
    {
    MessageBox.Show(error.Message);
    }
  }

All your doing is assigning a name and then showing the actual error message!


[IMG]http://i54.[censored].com/2je794x.jpg[/IMG]

Conclusion:
Try / Catch statements can help you out a ton. By using one you prevent program crashes. It makes you look like a more professional coder and it can save everyone a lot of hassle!
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
Nice tutorials you've been setting up! I'm kind of glad to see them. :P
You forgot about 'finally'.
Quote:Have you ever been running your application and all of a sudden something goes wrong? Your program crashes and you are left in a daze of confusion and sadness :(.


It's called a debugger. It's built into your IDE (most likely).

Quote:A Try/Catch statement will "Try" to execute a block of code. If something goes wrong, it will "Catch" the error and display it in a messageBox, rather than crashing.


That is not what the try-catch statement does. It is what your provided code does.
(Oct 21, 2010 01:28 AM)Zer0Fact0r Wrote:  It's called a debugger. It's built into your IDE (most likely).

Quote:A Try/Catch statement will "Try" to execute a block of code. If something goes wrong, it will "Catch" the error and display it in a messageBox, rather than crashing.

That is not what the try-catch statement does. It is what your provided code does.

Sometimes an error could happen, but not right on Form_Load. It is very possible that someone builds their application, not realizing there was an error until later.

Anyway, thats not the point. The point is to show a try/catch statement.
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by dummycartSep 9, 2021 03:38 AM 1 146 Sep 9, 2021 03:38 AM
Last Post: Lindros
[Python] by CiataLearnerDec 22, 2018 02:27 PM 1 209 Dec 22, 2018 02:27 PM
Last Post: notjapanese
[C] by Adam2017Dec 3, 2018 03:06 PM 2 167 Dec 3, 2018 03:06 PM
Last Post: Adam2017