DeathVenom
DeathVenom

Reputation: 394

How to just stop a method from running?

I'm making an app which is a fractions calculator. I have a method addNums(), which calls on another method fractionalize(). Now, fractionalize is within a try-catch, because that section can cause a lot of errors. Now the problem is that....If the try-catch gets an exception, I want addNums() to stop right there. It should not execute any further commands. How do I do that? Edit: Here's addNums():

public void addNums (View view){
        edit1 = findViewById(R.id.textview_number_1);
        edit2 = findViewById(R.id.textview_number_2);
        String frac1 = edit1.getText().toString();
        String frac2 = edit2.getText().toString();
        int[] fraction1 = fractionalize(frac1);
        int[] fraction2 = fractionalize(frac2);
        int num1 = 0, den1 = 0, num2 = 0, den2 = 0;
        num1 = fraction1[0];
        den1 = fraction1[1];
        num2 = fraction2[0];
        den2 = fraction2[1];
        int[] result = {0, 0, 0};
        int commonDen = getLCM(den1, den2);
        int addNum = num1 + num2;
        int[] rawResult = {addNum, commonDen};
        result = getResult(rawResult);
        if(simplify.isChecked()){
            result = simplify(result);
        }
        returnResult(result);
    }

And here's fractionalize():

public int[] fractionalize(String rawFraction1) {
        try {
            int[] result = {0, 0, 0};
            if (rawFraction1.contains("/") && !rawFraction1.contains(" ")) {
                //normal fraction
                int a = Integer.parseInt(rawFraction1.split("/")[0]);
                int b = Integer.parseInt(rawFraction1.split("/")[1]);
                result = new int[]{a, b, 0};
            } else if (rawFraction1.contains("/") && rawFraction1.contains(" ")) {
                //mixed fraction
                int wholeNum = Integer.parseInt(rawFraction1.split(" ")[0]);
                int num = Integer.parseInt(rawFraction1.split(" ")[1].split("/")[0]);
                int den = Integer.parseInt(rawFraction1.split(" ")[1].split("/")[1]);
                result = new int[]{num, den, wholeNum};
            } else if (!rawFraction1.contains(" ") && !rawFraction1.contains("/")) {
                int wholeNum = Integer.parseInt(rawFraction1);
            } else {
                Toast toast = Toast.makeText(getApplicationContext(), "Invalid input. Please see the correct format to enter numbers.", Toast.LENGTH_LONG);
                toast.show();
                result = new int[]{0,0,0};
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            Toast invalid = Toast.makeText(getApplicationContext(), "Invalid input, please see the correct format to enter numbers.", Toast.LENGTH_LONG);
            invalid.show();
        }
        return new int[] {0,0,0};
    }

Addnums() uses lots of other methods too but they're all right.

Upvotes: 0

Views: 36

Answers (2)

Erwin
Erwin

Reputation: 470

try this,

public void addNum() {
    try {
        System.out.println("before fractionalize");
        fractionalize();
        System.out.println("after fractionalize");
    } catch(Exception e) {
        e.printStackTrace();
    }
}

public void fractionalize() throws Exception {
    throw new Exception("exception in fractionalize");
}

Upvotes: 1

rzwitserloot
rzwitserloot

Reputation: 103018

Let the exception bubble up. Example:

void method1() {
    method2();
    method3();
}

void method2() {
    throw new RuntimeException();
}

void method3() {}

In the above example, invoking method1 results in method2 being invoked; that will stop executing when the exception is thrown, which causes method1 to stop execution as well, with method3 never even being invoked.

Upvotes: 1

Related Questions