using try/catch method to get a parameter that will make a certain output from a method in java -


i wrote code using try/catch, , want output this:

a null pointer exception occurred in block  length of string 1 number format exception occurred in block  null pointer exception occurred type of exception occurred in block  length of string 1 value 5 

but reason when call try call main error, question how can call method main output above?

this method wrote:

public static void problem2(string s) {     try {         int stringlen = s.length();         system.out.println("the length of string " + stringlen);         int = integer.parseint(s);         system.out.println("the value " + i);     } catch (numberformatexception e1) {         system.out.println("a number format exception occurred");     } catch (nullpointerexception e2) {         system.out.println("a null pointer exception occurred");     } catch (exception e3) {         system.out.println("some type of exception occurred");     } {         system.out.println("in block");     } } 

assuming placed static method in class "trycatchexample", following produce output examples 1, 2, , 4. since caught nullpointerexception, code not pass 1 catch block next. furthermore, why want to? caught more specific exception, might allow specific feedback user. more general exception less informative, , considered many bad approach.

public static void main(string[] args) {     trycatchexample.problem2(null);  // prints npe     trycatchexample.problem2("a"); // prints numberformat     // no example both numberformat , npe     trycatchexample.problem2("5"); // prints lengths & value 5 } 

Comments