Hack Forums
[C#] C# Try and Catch [Intermediate Level Tutorial] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] C# Try and Catch [Intermediate Level Tutorial] (/showthread.php?tid=755451)



C# Try and Catch [Intermediate Level Tutorial] - Pulser - Oct 20, 2010

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!


* - iPoison - Oct 20, 2010

Nice tutorials you've been setting up! I'm kind of glad to see them. :P


* - tflo - Oct 21, 2010

You forgot about 'finally'.


* - Zer0Fact0r - Oct 21, 2010

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.


* - Pulser - Oct 21, 2010

(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.