Chris Zog Chris Zog - 29 days ago 21
Java Question

Try - catch return method

is this syntax best method?

method 1:
input
method2():
method2: (input)
try
catch
return(method1())
method3: (input)
....


This is so when an exception is thrown you rarely want the program to crash, so it's best to have a return statement so the user can declare a new input for the variable. I am reading my textbook for class and they don't cover a return. Otherwise, without the return, having a try-catch seems pointless if the program just terminates.

Answer

Your syntax is simply vulnerable bacaues it can easily go stackoverflow by a malicious user's repeated inputs that cause the exception in method2.

How about this:

method 1:
  while
    input
    try
        return method2(input)
    catch
method2: (input)
    return (some result)
method3: (input)
   ....