Wolfish
Wolfish

Reputation: 970

Get the message of manually thrown exception

I'm deliberately throwing an exception for a particular scenario, but I would implicitly like to get the error message in string format. I'm aware that one of the overloads for the following exception is string message, but how do I access that string?

Here is the relevant snippet:

string errMsg;

private void Compress()
{
    if (sourcePath.EndsWith(".zip"))
    {
        throw new FileLoadException
                   ("File already compressed. Unzip the file and try again.");
        errMsg = //I want the above string here
    }
}

Upvotes: 3

Views: 2211

Answers (5)

BlackStarHH
BlackStarHH

Reputation: 75

here should be an solution for you :)

        if (sourcePath.EndsWith(".zip"))
        {
            FileLoadException ex = new FileLoadException("File already compressed. Unzip the file and try again.");
            errMsg = ex.ToString();
        }
        Console.WriteLine(errMsg);

To throw the exception that you made I would do soemthing like this

    static string sourcePath = "test.zip"; //hardcoded file
    static string errMsg; //the error string

    private static void Compress()
    {
        try
        {

            if (!sourcePath.EndsWith(".zip"))
            {
                Console.WriteLine("File doesn't end with zip so it can be compressed"); //if it does not end with zip its rdy for compressing and here you can indput your code to compress
            }
            else
            {
                throw new Exception(); //instead of using try catch you can also generate the code in here instead of throwing an exception.
                                       //when throwing a new exception you make it stop the if setting and jump into the catch if you use break it wont enter the catch but instead it will just jump over it.
            }
        }
        catch 
        {
            //Here it will generate the custom exception you wanted and put it inside the errMsg
            errMsg = new FileLoadException("File already compressed. Unzip the file and try again.").ToString();
            Console.WriteLine(errMsg);
        }

ofc this is made in console that is why I use the console.writeline you can just change those out

Upvotes: 0

Thomas
Thomas

Reputation: 2984

You can't access the string THERE I'll explain a bit there:

string errMsg;

private void Compress()
{
    if (sourcePath.EndsWith(".zip"))
    {
        throw new FileLoadException
                   ("File already compressed. Unzip the file and try again.");
        // The following line is unreachable as the throw ends the function and gets the exception to the calling function as there is no try catch block to catch the exception.
        errMsg = //I want the above string here
    }
}

A possibility would be to try/catch the exception in the method where you want to set the variable:

private void Compress()
{
    if (sourcePath.EndsWith(".zip"))
    {
        try
        {
            throw new FileLoadException
                       ("File already compressed. Unzip the file and try again.");
        }
        catch (Exception e)
        {
            errMsg = e.Message;
        }
    }
}

Or to catch the exception in the calling method instead:

try
{
    Compress();
}
catch (Exception e)
{

}

regardless of method used the e.Message gives you the message of the exception as a string.

Upvotes: 2

Joseph Jeganathan
Joseph Jeganathan

Reputation: 234

There is no point to try catch the exception and set the message. Unless you re-throw it

try
{
    throw new FileLoadException("File already compressed. Unzip the file and try again.");

}
catch (Exception ex)
{
    errMsg = ex.GetBaseException().Message;
    throw;
}

I would rather do this

private void Compress()
{
    if (sourcePath.EndsWith(".zip"))
    {
        errMsg = "File already compressed. Unzip the file and try again.";
        return;
    }
}

Upvotes: 1

Mohammad Mirmostafa
Mohammad Mirmostafa

Reputation: 1775

Do you mean this:?

try
{
    throw new FileLoadException
               ("File already compressed. Unzip the file and try again.");
}
catch (Exception ex)
{
    errMsg = ex.GetBaseException().Message;
}

Upvotes: 8

MaxJ
MaxJ

Reputation: 2095

Not sure I 100% understand but if you want the message from that exception you can catch it and examine the Exception.Message

        try
        {
            throw new FileLoadException("Custom error string");
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
        }

Upvotes: 0

Related Questions