Results 1 to 2 of 2

Thread: Java exceptions

  1. #1
    holtaf is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Question Java exceptions

    Hi guys ( and girls ); I have some problem writing my third program with Java ; I want to copy one file into another; This is my
    Java Code:
      1 import java.io.*;
      2 
      3 class IOTest {                  
      4 
      5         public static void main( String args[] ) throws IOException {
      6         
      7                 BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
      8                 
      9                 System.out.println( "Please insert file name ։ " );
     10                 String filename = br.readLine();
     11                 FileInputStream _if;
     12                 try {
     13                         _if = new FileInputStream( filename );
     14                 } catch ( FileNotFoundException e ) {
     15                 
     16                         System.out.println( "You inserted invalid file name, please try again ։ ");
     17                         filename = br.readLine();
     18                         throw new FileNotFoundException();
     19                         
     20                 }       
     21                 
     22                 System.out.println( "Now insert target file name ։ ");
     23                 filename = br.readLine();
     24                 FileOutputStream _of = new FileOutputStream( filename );
     25                 int byteval;
     26                 do {
     27                         _of.write( byteval = _if.read() );
     28                 } while( byteval != -1 );
     29                 
     30                 System.out.println( "Done!");
     31                 
     32                 _of.close();
     33                 _if.close();
     34                 
     35         }       
     36         
     37 }
    When I inserting valid file name its working fine, but if filename is invalid program terminating , with unhandled exception :
    Java Code:
    Please insert file name ։ 
    a
    You inserted invalid file name, please try again ։ 
    b
    Exception in thread "main" java.io.FileNotFoundException
    	at IOTest.main(IOTest.java:18)
    I want to regenerate exception and catch it when input is invalid;
    How can I do that ?

    Thanks in advice ;
    - holtaf

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default

    I want to regenerate exception and catch it when input is invalid;
    Why? I don't understand.
    I would think when you catch the exception, you would tell the user to enter a new name and try again.
    Why would you want to throw the exception again?
    Why not have a while loop that you don't exit until the user enters a valid input name or you exceed an attempt count.
    Java Code:
    boolean gettingName = true;  // true until we get a valid name
    while(gettingName) {
       ...
       // When you get a good filename
       gettingName = false; // turn off to allow us out of the loop
      ...
    }  // end while(gettingName)

Similar Threads

  1. Exceptions
    By Maya in forum New To Java
    Replies: 2
    Last Post: 05-24-2011, 07:30 AM
  2. Exceptions
    By Nerijus in forum New To Java
    Replies: 8
    Last Post: 05-18-2010, 02:44 PM
  3. Exceptions & More
    By besweeet in forum New To Java
    Replies: 12
    Last Post: 04-29-2010, 10:06 PM
  4. Client catching Web Service User Exceptions [Java 6.0.17]
    By Shaitan00 in forum Advanced Java
    Replies: 4
    Last Post: 11-22-2009, 02:09 AM
  5. Exceptions
    By hedonist in forum New To Java
    Replies: 10
    Last Post: 09-08-2009, 09:38 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •