Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
-
You can suppress warnings of unchecked exceptions by using
SuppressWarnings("unchecked")
but generally speaking, you cannot suppress warnings of checked exceptions.
-
You can use more than one
catch
blocks if necessary but you can only catch one exception in a catch block before Java 7. Starting from Java 7, you can catch multiple exceptions in a catch block. For example, The old style Java code} catch (FirstException ex) { logger.error(ex); throw ex; } catch (SecondException ex) { logger.error(ex); throw ex; }
becomes
} catch (FirstException | SecondException ex) { logger.error(ex); throw ex; }
in Java 7+.
-
To guarantee that some code will be run eventually, you have to put it into a
finally
block before Java 7. Starting from Java 7, filesystem resources are managed automatically so that you do not have to release them manually in afinally
block. For example, old style Java codeBufferedReader br = new BufferedReader(new FileReader(path)); try { return br.readLine(); } finally { br.close(); }
becomes
try (BufferedReader br = new BufferedReader(new FileReader(path)) { return br.readLine(); }
in Java 7.