简体   繁体   中英

Current state object - C#

For my current 'testing the waters' project, I'm trying to not use any Try-Catch blocks but instead catch each error (other than fatal) in other ways.

Now, when I say catch errors, my very contrived program makes one error which is easy to avoid; It tries to divide by 0 and this can be prevented by an If statement. To keep it simple I have only 1 C# file, with 1 class and two methods. I guess this is like a template, where the Constructor starts a process:

public class myObject
{
    public myObject()
    {
       Object objOne = methodOne();
       methodThree(objOne);
    }

    public object methodOne()
    {
        //logic to create a return object

        int x = 0;  

        //I've added a condition to ensure the maths is possible to avoid raising an exception when, for this example, it fails
        if (x > 0)
            int y = 5 / x;

        return object;
    }

    public void procesObjects(Object objOne)
    { 
        //logic  
    }    
}

So, as you can see in methodOne() I've added the if statement to ensure it checks that the maths isn't dividing by 0 . However, since I've caught it, my application continues which is not desired. I need a way to cease the application and log the failing for debugging.

So, this is what I think could work:

Create a class called Tracking which for this example, is very simple (or would a struct be better?).

public class Tracking
    {
        StringBuilder logMessage = new StringBuilder();
        bool readonly hasFailed;
    }

I can then update my code to:

public class myObject
{
    Tracking tracking = new Tracking();
    public myObject()
    {
       Object objOne = methodOne();
       if (!tracking.hasFailed)
           methodThree(objOne);
       if (tracking.hasFailed)
           ExteranlCallToLog(tracking);
    }
    public object methodOne()
    {
        //logic
        int x = 0;  
        //I've added a condition to ensure the maths is possible to avoid raising an exception when, for this example, it fails
        if (x > 0)
            int y = 5 / x;
        else
        {
            tracking.hasFailed = true;
            tracking.logMessage.AppendLine("Cannot divide by 0");
        }
         //may also need to check that the object is OK to return
        return object;
    }
    public void procesObjects(Object objOne)
    { 
        //logic  
    }    
}

So, I hope you can see what I'm trying to achieve but I have 3 questions.

  1. Should my tracking object (as it is in this example) be a class or a struct?
  2. I'm concerned my code is going to become very noisy. I'm wondering if when the system fails, it raises an event within the Tracking object which logs and then somehow closes the program would be better?
  3. Any other ideas are very welcome.

Again, I appreciate it may be simpler and easier to use Try-Catch blocks but I'm purposely trying to avoid them for my own education.

EDIT

The reason for the above was due to reading this blog: Vexing exceptions - Fabulous Adventures In Coding - Site Home - MSDN Blogs

Seriously, Dave - try catch blocks are there for a reason. Use them.

Reading between the lines, it looks like you want to track custom information when something goes wrong. Have you considered extending System.Exception to create your own bespoke implementation suited to your needs?

Something along the lines of:-

public class TrackingException : System.Exception 
{
   // put custom properties here.
}

That way, when you detect that something has gone wrong, you can still use try/catch handling, but throw an exception that contains pertinent information for your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM