javaexception

How to return value and Exception (if it is there.)


I have helper class in which I have written this function.

public static String createProject(Map<String, String> params,String projectName, String projectPrefix) {
        String createdProject = null;
        try {
             createdProject=//logic for creating createdProject string which may throw two exception mentioned below
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (TestLinkAPIException t) {
        t.printStackTrace();
    }
    return createdProject;
}

Now, I am calling this function from GUI part where I have written

String createProject=//called above function.

Now, If error occurs in above code, I want to show the error to the user.

My question is, how do I get back the created string and error message if some Exception occurs?


Solution

    1. Create a Custom Exception

    2. Add your String value as an instance field of that Custom Exception.

    3. Throw the custom exception with the String values passed in.

    Now you have the exception and the String as well.