[Tutorial] C# Exceptions and a debugging trick menu

Shout-Out

User Tag List

Results 1 to 2 of 2
  1. #1
    obfusk8's Avatar Member
    Reputation
    6
    Join Date
    Oct 2008
    Posts
    8
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Tutorial] C# Exceptions and a debugging trick

    I'm assuming the reader has at least some exposure to C#/.NET, or previous experience with exceptions. Also assuming that you use Visual Studio for development.

    My goal is to show you how to use exceptions and break()'ing to better troubleshoot problems in your software, and make your code easier to read and maintain.

    Most programmers seem to avoid learning error handling and debugging as long as possible. If you ever expect to write anything of any complexity, you have to have a grip on error handling.

    Try/Catch
    Try/Catch is a concept in programming of being able to logically state that:
    Code:
    Try [something]
        if [something] fails, 
    	do [something else]

    Logically similar to:

    Code:
    if(success)
    {
    	doSomething();
    }
    else
    {
    	doSomethingElse();
    }
    try/catch is a much more powerful and elegant of a solution than the above. The reasons why are way beyond this tut.

    So, I'm going to show an example of adding some error handling to a sample using the BlackMagic memory api. BlackMagic is available on the web, and is used in a lot of code around these forums. I picked it as an example for those reasons.

    In my sample code, I want a function that opens up the wow process for reading and writing. I want to wrap up a few lines of code in a function that will do this for me. We'll call it OpenWoWProcess()

    (for simplicity's sake, we're keeping the method signature simple. No args, void return)
    Code:
    private void OpenWoWProcess()
    {
    	    BlackMagic blackMagic = new BlackMagic(); 
                int processInt = SProcess.GetProcessFromWindowTitle("World of Warcraft");
                blackMagic.OpenProcessAndThread(processInt); 
    }

    Now, in our code, "OpenWoWProcess" gets called something like:

    Code:
    static void Main(string[] args)
    {
              OpenWoWProcess();
    }
    That's all great and everything... as long as nothing in OpenWoWProcess() ever fails, e.g. throws an exception. If it does, you application will die a horrific death. Let's say, for example, that the GetProcessFromWindowTitle() fails to find the WoW window. While running your application from Visual Studio, when that error occurs and that exception is thrown Visual Studio will stop and "break" on that line. (where is GetProcessFromWindowTitle() is called)

    So what now?
    Let's add some error handling and a little bit a Diagnostics code to better handle this problem. Please keep in mind, I'm not trying to FIX the problem in GetProcessFromWindowTitle(), I'm trying to make my application handle the unexpected a little better.

    First, back to my Main() process. Let's wrap the OpenWoWProcess() in a try/catch block.

    Code:
    try
    {
    	OpenWoWProcess();
    }
    catch(Exception exception)
    {
    	/* handle error */
    }
    So what's different?
    Now, when OpenWoWProcess() fails, any code in your catch block will be run. Additionally, you'll have a nice object called "exception" for you to inspect. Often times the information in the exception's properties is exactly what you need to figure out what's wrong. Exceptions, custom exceptions, properties, etc are a very big subject. Again, please refer to MSDN.

    Often times, you'll see logging performed in a catch block. For example:

    Code:
    try
    {
    	OpenWoWProcess();
    }
    catch(Exception exception)
    {
    	Log4Net.Log.instance(debug,"My OpenWoWProcess() failed",exception);
    }
    Or, you may want to pop a message box to the user:

    Code:
    try
    {
    	OpenWoWProcess();
    }
    catch(Exception exception)
    {
    	MessageBox.Show("My OpenWoWProcess() failed");
    }
    Both are valid, and can be used together.

    Finally, just a little trick that I do in this situation. Sometimes, you want your application to behave differently based on whether it's being run by as user (in Release mode), or by you (in Debug mode) while in Visual Studio.
    Here's a bit of code I use quite often:

    Code:
    if(System.Diagnostics.Debugger.IsAttached)
    	System.Diagnostics.Debugger.Break();
    What that code does is exactly what it says: When a debugger is running my code, break(), or pause, on that line.


    So, using it with our new way of handling our OpenWoWProcess() call allows us the luxury of giving our users a pretty message box, while explicitly break()'ing for us when we develop...all with the same code. (You could use pre-processor directives to accomplish a similar task, but that's old school )


    Code:
    try
    {
    	OpenWoWProcess();
    }
    catch(Exception exception)
    {
    	if(System.Diagnostics.Debugger.IsAttached)
    		System.Diagnostics.Debugger.Break();
    	else
    		MessageBox.Show("My OpenWoWProcess() failed");
    }
    That's it! The point was to give you a little knowledge in how to capture and handle errors in your code (or other people's code).
    Please, please, go to MSDN and learn about Exceptions. There's so much I didn't touch on like nested exceptions, custom types, exception properties, etc. Go learn!


    Sorry this got so long. I tried to keep it brief, but informative enough to read.
    Please keep in my mind this was psuedo-code basically, I never compiled or tested it. It shouldn't be copy/pasted. I made it up as I went along for example purposes.

    Hope you enjoyed the tutorial.

    Credits to Shynd's BlackMagic memory library used in my example. Shynd’s WoW Modification Journal
    Exception class on MSDN - Exception Class (System)
    C# try/catch MSDN - try-catch (C#)

    [Tutorial] C# Exceptions and a debugging trick
  2. #2
    Tegi's Avatar Active Member
    Reputation
    32
    Join Date
    Jan 2007
    Posts
    43
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your contribution, I'm confident that this will help a few, albeit it's quite short.
    Let me recommend the Wikipedia page on Exception handling for your readers, it gives a more general overview.
    +rep for the effort.

Similar Threads

  1. [Pally PvP] Tips and Tricks and Ret Pally Tricks
    By krazy1killa in forum World of Warcraft Guides
    Replies: 22
    Last Post: 05-30-2009, 11:58 AM
  2. Booty Bay and Ratchet Boat Trick
    By dukus in forum World of Warcraft Exploits
    Replies: 20
    Last Post: 10-14-2008, 09:43 PM
  3. [Tutorial] Find Any and All display ids in WoW
    By waakkit2 in forum WoW EMU Guides & Tutorials
    Replies: 5
    Last Post: 01-17-2008, 11:48 AM
  4. [Tutorial] See Sapph and Kel'Thuzad (Pics)
    By warbringer228 in forum World of Warcraft Model Editing
    Replies: 6
    Last Post: 05-03-2007, 05:08 PM
  5. Lightning Shield and Water Shield trick
    By GanjalfTheGreen in forum World of Warcraft General
    Replies: 3
    Last Post: 03-26-2007, 07:46 PM
All times are GMT -5. The time now is 01:39 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search