1

This is the skeleton of the code I have:

if(CheckForSomething())
{
    try
    {
        //do something
    }
    catch (UnauthorizedAccessException ex)
    {
        LogException(ex, item.server);
    }
    catch (Exception ex)
    {
        LogException(ex, item.server);
    }
}
else
{
    string error = "Some error";
    //want to call LogException with error as argument
}

private static void LogException(Exception ex)
{
    //write ex to one log file
    // write ex.message to another log file
}

How can I call LogException method from the else block? I tried casting string as exception and creating an exception.

7 Answers 7

9

A better question imo is not how but why you would want to do this? Why not define two LogError overloads, one that takes an Exception and another that takes a string.

private static void LogError(Exception ex)
{
    // write ex to one log file
    // write ex.message to another log file
}

private static void LogError(string error)
{
    //nothing to write to exception-specific log file
    // write error info to another log file
} 

It's not really advisable to generate your own Exception instance just for logging like this.

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

1 Comment

+1 for truth. Don't just answer a bad practice, encourage a good one.
8
LogException(new Exception("some error"));

4 Comments

Though you ought to be using a specific exception rather than the generic one.
waiting for the time limit to expire. I can't accept for another 3min.
@xbonez: While this does answer the question and will work for you i really suggest that you consider some of the other answers like Steve Townsend's and Profeten's. This answer is a hack that will allow you to do something that makes little sense. Honestly, if i was doing code review and saw something like this implemented, i would lose it.
@Paul: I did end up writing an overload for the method that handles strings specifically.
4

Have you considered factoring the // write ex.message to another log file behavior into a separate function and calling that with your desired string?

    if(CheckForSomething())
    {
        try
        {
            // do something
        }
        catch (UnauthorizedAccessException ex)
        {
            LogException(ex);
        }
        catch (Exception ex) // Never do this.  Do you know how to handle an OutOfMemoryException?
        {
            LogException(ex);
        }
    }
    else
    {
        string error = "Some error";
        LogMessage(error);
    }

private static void LogException(Exception ex)
{
    // write ex to one log file
    LogMessage(ex.Message);
}

private static void LogMessage(string message)
{
    // write message to another log file
}

Comments

3

you can also make you own exception class like this:

public class LogException: Exception
        {

            public LogException() : base()
            {
            }

            public LogException(string message) : base(message)
            {

            }

        }

Comments

1
Exception e = new Exception(error);      
LogException ee = new LogException (Exception e);
    throw ee;

Put this in the else block

Comments

1

You can do something like previously mentioned

LogException(new Exception("some error"));

But it might be better to create your own exception class:

class MyException : Exception 
{
    //...
}

then

LogException(new MyException());

Comments

0

Some good solutions so far. You don't want to create a custom exception just for logging someone else will have a WTF? moment reading your code.

The alternative is to just extract the message from exception for the log.... something like

if(CheckForSomething())
{
    try
    {
        // do something
    }
    catch (UnauthorizedAccessException ex)
    {
        LogMessage(ex.Message);
    }

}
else
{
    LogMessage("Some Error!");
}
private static void LogMessage(string message)
{
  //write message to log
}

Creating an exception for the sole purpose of passing it into a format handled by a method is a hack. It breaks the implicit understanding among programmers as to what exceptions are and what they are used for.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.