1

My Design Goals. There are two cases when Further code of execution stops in that code block 1. When some run time error occurs 2. Based on some business logic

When there is an error it will throw exception and I dont want to show it to business user. But when there is some business logic error/Validation failure then I want user to know as he/she can fix it and proceed further.

Example

Type 01: unable to cast to string Exception
Type 02: No Data is present in system against this criteria.

Code Block:

//int for Error Code to distinguish type 01 errors and type 02 errors.
//message for Error message
public int generateReport(ref string Message)
{
    try
    {
        //Fetch Data here
        if(no data present)
        {
            throw new Exception("No Data Present in system against this criteria").
        }
        string message = dataTable;
    }
}

So what is the best way for "Type 02 Exception" so that I will be able to communicate problem to user. And in case of "Type 01 Exception" I will simply say "Some Error Occured." and for developers/Support team I will show them exact reason for error without debugging facility they will be able to solve the problem.

2
  • can you not wrap the code in a try catch and display to the user a custom error message and log the e.Message or inner exception to the support team...? Commented Jun 9, 2014 at 18:14
  • Yes logging is one of the way but here I am not logging activity. Commented Jun 9, 2014 at 18:40

4 Answers 4

1

Generally you should try not to use Exceptions for business logic flow, they're designed to be 'Exceptions' to the rule. You could instead use a return type:

public Status GenerateReport()
{
    if(no data present)
        return Status.NoData;
}

At this point you can use a lookup to go and find an appropriate message to display to that user. If you really want to use Exceptions, then create your own:

public class ReportException : Exception
{
    public ReportException(String message) : base(message)
    {}
}

At this point you can catch just ReportExceptions to to display messages to the user and ignore everything else. To improve this exception further you could instead provide an enum into the constructor, so the message is looked up:

public ReportException(Status status) { }

public String ReportStatus
{
  get { return messages[this.status]; }
}

The advantage here is that it makes it easier to localize the messages if your application every requires additional languages.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. 2nd suggestion is something that is good option. But here I want to keep everything related to this function into this function only so that whenever I call this function I dont need to manage each Status type in calling function. Calling function should get whatever is applicable in this case and based on Return value it will determine the Execution result of the called function and proceed further with either throwing exception or showing a message whatever applicable.
0

Traditionally, your "type 2 exception" shouldn't be an exception at all. Exceptions are for exceptional circumstances - they occur when the program can't execute as expected, i.e. a situation such as your first example; a situation that ideally should never occur.

Your second example represents a situation that often will happen - the user is looking for something that just isn't there. That's expected so isn't cause for exceptions. In that situation, you should just return a value indicating that there is no data, then check for this value and display an appropriate message.

In some instances you may also want to log that this has occurred so you have an audit trail of user interactions, which will allow you to identify common patterns and/or explain after-the-fact why the user didn't find what he/she was looking for.

Comments

0

I agree with the rest, Exceptions are not supposed to control workflow and should not be used to drive business logic. With that said here are a couple of options to assist but of course there are many ways to skin this cat.

Code Contracts: for .net 4.5+

http://msdn.microsoft.com/en-us/library/dd264808(v=vs.110).aspx

Validation: Le has an interesting way of handling this...

http://blog.longle.net/2013/06/03/building-an-extensible-fluent-validation-framework-using-generic-funcs-and-wiring-it-up-to-mvc-4/

Another validator..

https://fluentvalidation.codeplex.com/

Enterprise Library Validation:

http://www.codeproject.com/Articles/256355/Microsoft-Enterprise-Library-Introduction-to-V

Hope this helps... Le's is a pretty easy implementation.

3 Comments

FYI: Link 2 is expired.
hmm might be network on your side, they are all working for me still.
Working again. It was expired for half a day, apparently, according to the timestamps of the comments.
0

Exceptions are used for exceptional cases. Read my article: What are exceptions

In your case you say that it's valid that no data is found, in that case you can communicate that by using a empty report (like string.Empty):

public Report GenerateReport(string message)
{
    if(no data present)
    {
        return Report.Empty;
    }

    // create report here
    return generatedReport;
}

Which uses a static readonly field in the report class:

public class Report
{
    public static readonly Report Empty = new Report("This report is empty");

    // read of the class like constructors etc.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.