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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:22:53+00:00 2026-05-31T01:22:53+00:00

This may be a stupid question, but I’m interested in the performance of using

  • 0

This may be a stupid question, but I’m interested in the performance of using try/catch blocks.

I have a DataGrid that assigns a Converter to the background property of a DataGridCell. In the converter, I compare the value of this year’s data to last year’s data; if this year’s data is > 3%, I return a Green background; if it’s > 0% and < 3%, I return a Yellow; and if it’s < 0%, I return Red:

string x = values[0].ToString().Replace("$", "").Replace(",", ""); //This year's number
string y = values[1].ToString().Replace("$", "").Replace(",", ""); //Last year's

result = (((float.Parse(x) * 100) / float.Parse(y)) - 1) * 100;

if (result >= 3)
    return Brushes.LimeGreen;
else if (result >= 0)
    return Brushes.Yellow;
else
    return Brushes.Red;

However, in some cases, the cell will NOT have a value of last year; as you can guess, dividing by 0 (or some text as the Converter seems to receive when the Cell is empty) is a pretty bad idea and will throw an exception. So, I decided the easiest way to deal with this was:

try
{
    result = (((float.Parse(x) * 100) / float.Parse(y)) - 1) * 100;
}
catch
{
    return Brushes.DarkOrange;
}

So if the exception is thrown (whereby there is no value to compare to), return an Orange and call it a day. (edit: yes, I do wish to return an Orange when there is no value to compare to.

Currently, I can predict that it will only happen to one row of data for now, so it only catches about 10 cells when it’s populated. But as the future goes on, it has the possibility of having it happen more times.

The try/catch block is the easiest and quickest way to handle this(as far as I can tell), but it is obviously not the only way, especially since I know the error. So Is using a try/catch block a bad idea in this case? And by bad idea, I mean will it slow performance since it is iterated over many, many times? Given that I know what the error will be, should I preempt it, or is using a try/catch block fine?

  • 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-05-31T01:22:54+00:00Added an answer on May 31, 2026 at 1:22 am

    This is a bad idea. It’s not an exceptional situation, so don’t handle it like it’s one. The fact that you’re worried about performance and exception handling is a smell that you’re doing something wrong. There is a really simple way to handle this, so I don’t even see why you ever thought to instead handle it using exception handling.

    Yes, it will impact your performance but that shouldn’t be your concern here. Your concern should be what is the clearest way to write this logic, and using exceptions is clearly not the way.

    Here’s that simple way:

    double value;
    if(!Float.TryParse(y, out value) || value == 0f) {
        return Brushes.DarkOrange;
    }
    else {
        double result = (((float.Parse(x) * 100) / value) - 1) * 100;
        if (result >= 3) {
            return Brushes.LimeGreen;
        }
        else if (result >= 0) {
           return Brushes.Yellow;
        }
        else {
            return Brushes.Red;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be a stupid question, but I have to ask! I have the
This may be a stupid question but one that is throwing me for a
I know this may be a stupid question to ask but I have really
I have a feeling this may be stupid question, but I can't come to
This may be a very stupid question, but... I'm using JPA 2.0 with Hibernate
This may be a stupid question, but here goes. I've seen several projects using
this may seem like a stupid question but I have a query string which
This may seem like a stupid Question but i have two cubes being rendered
This may be a stupid question, but it's something that bugs me on a
This may be a very stupid question, but as I'll try it anyway. I'm

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.