I'm having a hard time solving problems using try-catch syntax in Java

78 views Asked by At

If the length of the string exceeds 20 because the string is inputted with str

Generates RuntimeException with string "More than 20 words"

If str is null, it will generate RuntimeException with string "null"

If this is not the case in either case, write a len_check function that returns the received string as it is.

Example output.

System.out.println(len_check("abcdefghijklmnopqrs"));
abcdefghijklmnopqrs
System.out.println(len_check("abcdefghijklmnopqrsuvw"));
Exception in thread "main" java.lang.RuntimeException: More than 20 words
    at ...
System.out.println(len_check(null));
Exception in thread "main" java.lang.RuntimeException: null
    at ...
import java.lang.*;

class Main {

    public static String len_check(String str) {
        try {
            if (str.length() > 20) {
                throw new RuntimeException("Length is greater than 20");
            } else if (str == null) {
                throw new RuntimeException("null");
            } else {
                return str;
            }
        } catch (RuntimeException e) {
            throw e;
        }
    }

    public static void main(String[] args) {
        System.out.println(len_check("abcdefghijklmnopqrs"));// abcdefghijklmnopqrs
        System.out.println(len_check("abcdefghijklmnopqrsuvw"));
        System.out.println(len_check(null));
    }
}

An error occurs in the second print statement while being output (this is my intended error) and stops. I want it to be printed out until the last error.

2

There are 2 answers

1
smallpepperz On BEST ANSWER

To prevent the program from exiting after the first error, you should use a try-catch block on the calls themselves, not inside the function where they're thrown. If you handle them in the same place you throw them, you may as well not be throwing them at all, and if you try and handle all three function calls within the same try-catch block, it will exit after the first error.

import java.lang.*;

class Main {
    public static String len_check(String str) {
        if (str.length() > 20) {
            throw new RuntimeException("Length is greater than 20");
        } else if (str == null) {
            throw new RuntimeException("null");
        } else {
            return str;
        }
    }

    public static void main(String[] args) {
        try {
            System.out.println(len_check("abcdefghijklmnopqrs"));// abcdefghijklmnopqrs
        } catch (Exception e) {
            e.printStackTrace()
        }

        try {
            System.out.println(len_check("abcdefghijklmnopqrsuvw"));
        } catch (Exception e) {
            e.printStackTrace()
        }

        try {
            System.out.println(len_check(null));
        } catch (Exception e) {
            e.printStackTrace()
        }
    }
}
0
Reilas On

The try-catch block isn't necessary in the len_check method.
And, you'll need to re-factor the if-statements, checking null first.

static String len_check(String str) {
    if (str == null) throw new RuntimeException("null");
    else if (str.length() > 20)
        throw new RuntimeException("Length is greater than 20");
    return str;
}

If you want to test different values, you'll need to contain each len_check call within a try-catch.

try {
    System.out.println(len_check("abcdefghijklmnopqrs"));// abcdefghijklmnopqrs
} catch (Exception e) {
    e.printStackTrace();
}
try {
    System.out.println(len_check("abcdefghijklmnopqrsuvw"));
} catch (Exception e) {
    e.printStackTrace();
}
try {
    System.out.println(len_check(null));
} catch (Exception e) {
    e.printStackTrace();
}

Output

abcdefghijklmnopqrs
java.lang.RuntimeException: Length is greater than 20
    at Main.len_check(Main.java:17)
    at Main.main(Main.java:8)
java.lang.RuntimeException: null
    at Main.len_check(Main.java:15)
    at Main.main(Main.java:8)