Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7797715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:46:24+00:00 2026-06-01T23:46:24+00:00

I’m going to provide a simple example of what I’m trying to do —

  • 0

I’m going to provide a simple example of what I’m trying to do — hopefully it is possible?

I basically have a class that does a whole ton of formatting/analyzing to the data. As a result, there a lot of things that can go wrong with this. The problem I have is handling the class when things go wrong. I want all execution of this class to stop once an error has occurred.

This class (AnalyzingStuff) is called from a parent form that does various things based on the result of this classes execution.

Ideally, I would fire an event named say “ABORT”.

So in this code here I do the following:

Class AnalyzingStuff{

public event EventHandler ABORT;

public AnalyzingStuff(){

    for(int i = 0; i < 999999; i ++){
        AnalyzeSomeStuff();
        AnalyzerSomeOtherStuff();
    }
    MoreStuff();
    OtherStuff();
}

private void AnalyzeSomeStuff(){
   if(someconditionNotMet){
        //EXIT OUT OF THIS CLASS, STOP EXECUTION!!!
        this.ABORT.Invoke(this, null);
   }
}
}

Calling this ‘ABORT’ event, I would stop the execution of this class (stop the loop and not do anything else). I could also catch this event handler in some other parent form. Unfortunately, I can’t find any way of stopping the execution of this class.

Ideas so far:

  1. The obvious answer is to simply set a flag and constantly check this flag over and over in multiple places, but I really don’t like this approach (my current implementation). Having to check this after every single method call (there are MANY) is ugly codewise.

  2. I thought maybe a background worker or something where you could cancel the execution of the DoWork?

  3. Use a form as a base class for the AnalyzingStuff so I can simply call “this.Close();”.

What do you think is the best approach to this situation? Are these the best solutions? Are there any other elegant solutions to what I want here or am I going completely in the wrong direction?

EDIT: I have a series of try/catch blocks used throughout this code that is used to handle different errors that can occur. Unfortunately, not all of them call for an Abort to occur so they need to be caught immediately. Therefore, try/catch not the most ideal approach.. or is it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:46:26+00:00Added an answer on June 1, 2026 at 11:46 pm

    Don’t do analysys in the constructor. Do it in a main Analyze() method.

    Use exceptions. If you want to abort because of a fatal error, throw a fatal exception. That is, throw an exception that you don’t catch within the scope of the main analysis method.

    class Analyzer
    {
        public Analyzer()
        {
            // initialize things                
        }
    
        public void Analyze()
        {
            // never catch a fatal exception here
            try
            {
                AnalyzeStuff();
                ... optionally call more methods here ...
            }
            catch (NonFatalException e)
            {
                // handle non fatal exception
            }
    
            ... optionally call more methods (wrapped in try..catch) here ...
        }
    
        private void AnalyzeStuff()
        {
            // do stuff
            if (something nonfatal happens)
                throw new NonFatalException();
    
            if (something fatal happens)
                throw new FatalException();
        }
    }
    

    outside:

    {
        var analyzer = new Analyzer();
        try
        {
            analyzer.Analyze();
        }
        catch (FatalException)
        {
            Console.WriteLine("Analysis failed");
        }
    }
    

    If you don’t like using exception this way, you can accomplish the same thing by having every analysis method return a bool:

    if (!AnalyzeStuff())
        return false;
    if (!AnalyzeMoreStuff())
        return false;
    ...
    return true;
    

    But you end up with a lot of return statements or a lot of braces. It’s a matter of style and preference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.