Question

I have a situation in which a method should return two messages if there is an exception in try, catch block and it should return a single success message if there is no exception.

I'm calling this method in a different class and assigning its result to a string. I can concatenate the exception method and send it as a single message to this class. But, i have to split this message again.

Is there any way to split a string message?

sample code:

public class sendMessage
    sub main()
        method()
    end sub

    public function method() as string
        try
            ------------
            if response isnot nothing
                message= "success"
            end if
       catch
           message="failure"+" "
           message+=ex.tostring()
       end try
       return message
    end function 
end class


public class invokeMessage
    sub main()
        dim ob as new sendMessage
        dim message = ob.method()
        dim messageStatus=? ------------this can be success or failure
        dim messageException ----------- this has exception message in case of failure
    end sub
end class
Was it helpful?

Solution

If you need to return multiple values from the function, then:

1 - Define a return type

public class MyReturnResult
{
    public bool IsException;
    public string Message;
}

public MyReturnResult MyMethod()
{
    try
    {
       ...
    }
    catch (Exception e)
    {
        return new MyReturnResult() {IsException = true, Message = e.ToString()};
    }
    // can be inside try
    return new MyReturnResult() {IsException = false, Message = "Ok"};
}

2 - Use famous GetLastError theme

public string LastMessage;

public bool MyMethod()
{
    try
    {
       ...
    }
    catch (Exception e)
    {
        LastMessage = e.ToString();
        return true;
    }
    LastMessage = "Ok";
    return false; // no exception
}

3 - Use out parameters

// or make bool function and string out parameter
public string MyMethod(out bool isException)
{
    try
    {
       ...
    }
    catch (Exception e)
    {
        isException = true;
        return e.ToString();
    }
    isException = false;
    return "Ok";
}

In your case you may define special string (to example, "blablablaOk" or with null value) to indicate no exception case, otherwise it would indicate what there was exception and would contain different from that exception message.

public string MyMethod()
{
    try
    {
       ...
    }
    catch (Exception e)
    {
        return e.ToString();
    }
    return "blablablaOk";
}

and then you check

if(MyMethod() == "blablablaOk")
{
    // no exception
}
else
{
}

OTHER TIPS

public class sendMessage
    sub main()
        method()
    end sub

    public function method() as string
        try
            ------------
            if response isnot nothing
            message= "success"
            end if
       catch
           message="failure"+" "
           message+=ex.tostring()
       end try
       return message
    end function 
end class


public class invokeMessage
    sub main()
        dim ob as new sendMessage
        dim message = ob.method()
        if message.contains(separator)
            dim messages() as string = message.split(separator) // array of 2 elements with "failure" + "rest of the message"
   end sub
end class
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top