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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:51:39+00:00 2026-05-16T15:51:39+00:00

static class MyClass { static void Main() { bool b1 = false; if (

  • 0
static class MyClass
{
    static void Main()
    {
        bool b1 = false;
        if ( b1 )
        {
            throw new MyException(GetText(b1));
        }
    }

    public static string GetText(bool bFlag)
    {
        if ( bFlag==false )
        {
            throw new Exception("'GetText' method should not be called when condition is 'false');
        }
        return "Some error text";
    }
}

All works fine in example above (I mean when condition b1 is ‘True’ then ‘MyException’ will be generated with a proper text. And when condition if ‘False’ then nothing will be happened

I want to have some helper method for my exception class:

class MyException : Exception
{
    public void FailIfTrue(bool bCondition, string strErrorMessage)
    {
        if ( bCondition )
        {
            throw new Exception(strErrorMessage);
        }
    }
}

In this case “Main” method would be changed to another one:

static class MyClass
{
    static void Main()
    {
        bool b1 = false;
        MyException.FailIfTrue(
            b1, 
            GetText(b1)
            );
    }
}

In this case ‘GetText’ method will be called…

Question:

Do you see any good workaround or solution to create a helper that will call ‘GetText’ only when it’s result is required inside of ‘FailIfTrue’ function?

Any thoughts are welcome.

Thank you.

  • 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-16T15:51:40+00:00Added an answer on May 16, 2026 at 3:51 pm

    Actually, I see a workaround (pass not a string parameter, but a Func):

    public void FailIfTrue(bool bCondition, Func<string> funcErrorMessage)
    {
        if ( bCondition )
        {
            throw new Exception(funcErrorMessage());
        }
    }
    

    But not sure if it is the best what is possible here.

    Please advise!

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

Sidebar

Related Questions

If class MyClass { public static void main(String[] str) { System.out.println(hello world); } }
import java.util.*; public class MyClass { public static void main(String[] args) { List<String> a
Possible Duplicate: Is Java pass by reference? public class myClass{ public static void main(String[]
import javax.swing.*; import java.awt.*; public class MyClass { public static void main(String args[]) {
Here is a piece of code: private class myClass { public static void Main()
class MyClass { public: void method2() { static int i; ... } }; Will
Consider the following code: public class MyClass { public static string MyStaticMethod() { //string
This isn't valid code: public class MyClass { private static boolean yesNo = false;
public class MyClass { public static void method() { try { // there is
abstract class MyClass{ static class StaticClass{ public void showAdd(){ System.out.println( show add); } }

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.