简体   繁体   中英

How can I create my own exception?

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.

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.

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

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
}

you can also make you own exception class like this:

public class LogException: Exception
        {

            public LogException() : base()
            {
            }

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

            }

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

Put this in the else block

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());

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM