A cosa serve throws in questo codice, si può eliminare?

di il
10 risposte

A cosa serve throws in questo codice, si può eliminare?

A cosa serve throws in questo codice, si può eliminare?
Tutto il codice di main() è già impacchettato in una serie di blocchi try/catch così non riesco a capire a cosa possa servire throws e mi chiedo se sia lecito togliere il pezzetto "throws IOException" lasciando il resto invariato senza però compromettere il funzionamento e la robustezza del programma. In pratica mi sto chiedendo se throws IOException sia stato messo per qualche ragione oppure se sia una svista di Herbert Schildt, autore del mio manuale base di java.
/* Copy a text file. 
   To use this program, specify the name 
   of the source file and the destination file. 
   For example, to copy a file called FIRST.TXT 
   to a file called SECOND.TXT, use the following 
   command line. 
 
   java CopyFile FIRST.TXT SECOND.TXT 
*/ 
 
import java.io.*; 
 
class CopyFile { 
  public static void main(String args[]) throws IOException  
  { 
    int i; 
    FileInputStream fin = null; 
    FileOutputStream fout = null; 
 
    // First, confirm that both files has been specified. 
    if(args.length != 2) { 
      System.out.println("Usage: CopyFile from to"); 
      return; 
    } 
 
    // Copy a File. 
    try { 
      // Attempt to open the files. 
      fin = new FileInputStream(args[0]); 
      fout = new FileOutputStream(args[1]); 
 
      do { 
        i = fin.read(); 
        if(i != -1) fout.write(i); 
      } while(i != -1); 
 
    } catch(IOException e) { 
      System.out.println("I/O Error: " + e); 
    } finally { 
      try { 
        if(fin != null) fin.close(); 
      } catch(IOException e2) { 
        System.out.println("Error Closing Input File"); 
      } 
      try { 
        if(fout != null) fout.close(); 
      } catch(IOException e2) { 
        System.out.println("Error Closing Output File"); 
      } 
    } 
  } 
}

10 Risposte

Devi accedere o registrarti per scrivere nel forum
10 risposte