简体   繁体   中英

Java Verifying in Separate Classes

Sorry for bad title, but I don't know how to phrase simple problem.

I have inside a class, a static function that returns an array of that class. Eg

//returns CustomStack[]
CustomStack.createCustomStacks({{1,2},{3,4}})

Then I have a separate class that wants to use this static function for some other purpose. The problem I'm having is deciding how I should handle errors and exceptions.

So given this function in another separate class:

public boolean StackConfirmer (CustomStack[] myCustomStacks) {
    ....
}

How do I change this function/the output of the CustomStack.createCustomStacks so that I can also handle errors? I can clarify/edit as needed if I'm still unclear. Apologies English is not my first language. Like do I use null? Do I use -1?

EDIT: Here's a bit more elaborated code:

public class CustomStack {
    ...constructors and other irrelevant functions...

    public static CustomStack[] createCustomStacks (some_input) {
        if some_input is valid -> return CustomStack[] with Stacks inside
        if some_input isn't valid -> I'm not sure what to do.  Raise exception?
    }
}

public class ProgramThatUsesCustomStacks {
    ...
    private boolean StackConfirmer (CustomStack[] myCustomStacks) {
        // How should I be checking if there was a valid input in the other class?
        ....
}

I would suggest using -1 as your first element in the array (assuming it would never have -1 without an error). This will be your flag that an error occurred, then you can pad the array to a particular length to communicate the nature of the error. ie an array consisting of {-1, 0} if you got bad data, {-1, 0, 0} if you got too much, etc.

You can try creating your own Exception class and then throw it in your static method, for example: "CustomStackException extends Exception" and then handle the specific potential issues inside of that. Just a suggestion, it would be clean.

From your description I imagine you have a code like this

class SeparateClass{
    public boolean StackConfirmer (CustomStack[] myCustomStacks) {
        int[][]x={{1,2},{3,4}};
        CustomStack.createCustomStacks(x);
        return true;
    }
}

class CustomStack{
    static CustomStack[] createCustomStacks(int[][]x){
        return null;
    }
}

Say that in your createCustomStacks method you want to make sure that no null value is passed as function input. You can throw an exception to be handled by the caller SeparateClass.StackConfimer() .

To give you an idea, this is how you should update your code

class SeparateClass{
    public boolean StackConfirmer (CustomStack[] myCustomStacks) {
        int[][]x={{1,2},{3,4}};
        try {
            CustomStack.createCustomStacks(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}

class CustomStack{
    static CustomStack[] createCustomStacks(int[][]x) throws Exception{
        if(x==null)throw new Exception();
        return null;
    }
}

Note that I decided to throw a generic Exception object just because that's a sample code to give you a first idea. There are a lot of more specific exceptions you can use, or you can define your owns by extending Exception class

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