开发者

Return catched exception inside a class method to the caller

I am making a class for using in a winforms application in VC#

My question is how to return a catched exception to the caller out of the class? Take this as an example:

Public Class test
{
    private int i = 0;

    public test() { }

    public SetInt()
    {
        try
        {
            i = "OLAGH"; //This is bad!!!
            return i;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

And imagine calling this method in another place while referencing to this class. Is that a good idea? Or how it开发者_JAVA百科 should be done?


You have several options.

  • You could not handle the exception at all :

    public SetInt()
    {
        i = "OLAGH"; //This is bad!!!
        return i;
    }
    

Then the caller will need to handle the exception.

  • If you want to handle the exception you can catch the error and handle it.

    public SetInt()
    {
        try
        {
            i = "OLAGH"; //This is bad!!!
            return i;
        }
        catch (FailException ex)
        {
            return FAIL;
        } 
    }
    

Note that it is bad practice to just catch the base Exception class. You should anticipate which errors may occur and try to handle them. Unanticipated errors and the result of bugs and should make a big noise so that you can be alerted to other problems and fix them.

  • If you want to raise your own kind of exception, you could do :

    public SetInt()
    {
        try
        {
            i = "OLAGH"; //This is bad!!!
            return i;
        }
        catch (FailException ex)
        {
            throw new SetIntFailException ( ex );
        } 
    }
    

Then it is the callers responsibility to handle the SetIntFailException rather than a CastFailException or whatever hundreds of other kind of exceptions your code may throw..

  • If you want the caller to handle the exception, but you have some clean up you want to do, you can use finally :

    public SetInt()
    {
        try
        {
            i = "OLAGH"; //This is bad!!!
            return i;
        }
        finally
        {
            // Cleanup.
        } 
    }
    

The code in the finally block will always be called, even when there is an exception, but the error still gets raised to the caller.

I am assuming that in your real code, it will at least compile! ;-)


In the first place, the code won't compile.

public class test
{
    private int i = 0;

    public test(){}

    public SetInt(object obj)
    {
        try
        {
            i = (int) obj;
            return i;
        }
        catch(exception ex)
        {
           throw; // This is enough. throwing ex resets the stack trace. This maintains it
        }
    }
}

If you want to throw an exception do this:

throw new Exception ("My exception");

You can make a class derived from Exception if you want to hold some exception specific details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜