Advertisement
sergAccount

Untitled

Aug 30th, 2020
1,375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.spec;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.FileReader;
  10. import java.io.IOException;
  11.  
  12. /**
  13.  *
  14.  * @author Admin
  15.  */
  16. public class Test3 {
  17.  
  18.     public static String getString() throws Exception {
  19.         try ( BufferedReader br = new BufferedReader(new FileReader("some path"))) {
  20.             return br.readLine();
  21.         }
  22.     }
  23.  
  24.     public static void main(String[] args) {
  25.         // try-with-resource
  26.         try (MyResource resource = new MyResource()) {
  27.             resource.doSomething();
  28.         }catch(Exception ex){
  29.             ex.printStackTrace();
  30.         }
  31.         //  try-catch-finnaly
  32.         MyResource resource2 = new MyResource();
  33.         try {
  34.             resource2.doSomething();
  35.         }catch(Exception ex){
  36.             ex.printStackTrace();
  37.         }finally{
  38.             try{
  39.                 resource2.close();
  40.             }catch(IOException ex){
  41.                 ex.printStackTrace();
  42.             }
  43.         }        
  44.     }
  45.  
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement