J7TryCatch.java 938 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import java.io.DataOutputStream;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. public class J7TryCatch {
  5. public void simpleTry() {
  6. try {
  7. } catch (Exception e) {
  8. } finally {
  9. }
  10. }
  11. public void tryWithResource(FileOutputStream fos) {
  12. try (DataOutputStream dos = new DataOutputStream(fos)) {
  13. dos.writeUTF("Java 7 Block Buster");
  14. } catch (IOException e) {
  15. }
  16. }
  17. public void tryWithResources() {
  18. try (FileOutputStream fos = new FileOutputStream("movies.txt"); DataOutputStream dos = new DataOutputStream(fos)) {
  19. dos.writeUTF("Java 7 Block Buster");
  20. } catch (IOException e) {
  21. }
  22. }
  23. public void newMultiCatch() {
  24. try {
  25. methodThatThrowsThreeExceptions();
  26. } catch (ExceptionOne | ExceptionTwo | ExceptionThree e) {
  27. }
  28. }
  29. public void newMultiMultiCatch() {
  30. try {
  31. methodThatThrowsThreeExceptions();
  32. } catch (ExceptionOne e) {
  33. } catch (ExceptionTwo | ExceptionThree e) {
  34. }
  35. }
  36. }