Most of the time a try block is accompanied by a catch block that catches the java.lang.Exception in addition to other catch blocks.
The order of the catch blocks is important.
The more specific exception type appears first.
public class MainClass {
public static void main(String[] args) {
String input = null;
try {
String capitalized = capitalize(input);
System.out.println(capitalized);
} catch (NullPointerException e) {
System.out.println(e.toString());
}
}
public static String capitalize(String s) throws NullPointerException {
if (s == null) {
throw new NullPointerException("Your passed a null argument");
}
Character firstChar = s.charAt(0);
String theRest = s.substring(1);
return firstChar.toString().toUpperCase() + theRest;
}
}