Search code examples
javatry-catchruntimeexception

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


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.


Solution

  • 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()
            }
        }
    }