Simple Java Exception Handling Tips
Exceptions are errors that occur in runtime , that is , when the application is running. A careful developer should estimate and care about possible runtime errors. Let us give an example : String s = args[0]; int i = Integer.parseInt(s); this piece of code can cause some exceptions. For example args is an array of string. We want to read the first element of this array. What happens if args is null ? What happens if args is empty ? so we need to ensure that the array is not null and not empty. String s = null ; // initialize s // check the array is not null or empty if ( args ! = null && args. length > 0 ) s = args [ 0 ] ; int i = 0 ; if ( s ! = null ) // if s is not null we can parse i = Integer . parseInt ( s ) ; ...