Monday 28 July 2014

Java 7 : Exception handling

Highlights:

- Performs more precise and intelligent analysis while re-throwing the exception
- Improved type checking while throwing exception
- Simpler handling for more then one type of exceptions


Examples:

Please find below examples for exception handling for more clarification:

    /* Very generic exception handling in catch block , but precise handling in throws statement*/
     public void rethrowException(boolean flag) throws IOException,
    FileNotFoundException {
        try {
            if (flag) {
                throw new IOException();
            } else {
                throw new FileNotFoundException();
            }
        } catch (Exception e) {
            throw e;
        }
    }

/* No throw required for method , because it can analyze no exception thrown*/
    public void rethrowException() {
        try {
            try {
                throw new Exception();
            } catch (Exception e) {
            }
        } catch (Exception e1) {
            throw e1;
        }
    }
  

    /* Very generic exception handling in catch block , but precise handling in throws statement*/
    public static void rethrow() throws ParseException{
        try {
            new SimpleDateFormat("yyyyMMdd").parse("1");
        } catch (Exception e) {
            throw e;
        }
    }