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 8774643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:37:12+00:00 2026-06-13T18:37:12+00:00

For my current ‘testing the waters’ project, I’m trying to not use any Try-Catch

  • 0

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

  • 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-13T18:37:13+00:00Added an answer on June 13, 2026 at 6:37 pm

    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.

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

Sidebar

Related Questions

current i am trying to use ajax for my site ....... because of size
current code (not working): /^script\s*type=\text\/javascript/i.test(tagName)
Current demo here: http://www.studioimbrue.com/sites/eliotdudik I have the tabs system working wonderfully. I'm trying to
Current Project Setup I've been working on a web-based chat, similar to Facebook chat.
Current Code: WHERE EXTRACT(YEAR_MONTH FROM timestamp_field) = EXTRACT(YEAR_MONTH FROM now())) Instead of EXTRACT(YEAR_MONTH FROM
Current project is an Mvc4 application, I had Ioc working and recently it just
Current image looks like <img src=images/example.jpg /> Now if img src do not have
Current Asynchronous JavaScript calls require us to use a Callback function. This can lead
Current Regex: ^([\d-]*[A-z]?|[A-z]?[\d-]*|[\d-]*[A-z]?[\d-]*){3}$ (Allows a max of 3 letters and any amount of numbers
Current project: To make a time program in C# with two classes Time and

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.