Getting try / catch for FileNotFoundException

Well shocker, shocker, I’m back with another newbie question!

I’m currently working on a program which reads information from a file, and it won’t let me compile without a FileNotFoundException, and I’m a little uncertain how to set up the Try / Throw / Catch thing.

Right now I’m stuck at


	try {
               Code Here
	} catch(FileNotFoundException) {
    throw new FileNotFoundException("Error.");
    }

But that’s not working quite right. Any tips on how to fix will be appreciated :slight_smile:

You have to give a name to your exception too :slight_smile:


try {
    //Code Here
} catch(FileNotFoundException fileEx) {
    throw new FileNotFoundException(fileEx);
}

Your method also has to signify that it can throw that type of exception, example:


public boolean isReadableFile (File f) throws FileNotFoundException
{
//code in here
}

Thanks. I’m a little uncertain about where exactly to signify the latter for the method, as far as I can tell the right place would be somewhere in this line :


	Scanner fra_fil = new Scanner (new File("kryssordfil.txt"));

Here’s my entire code if that’s any help:


import java.io.*;
import java.text.*;
import java.util.Scanner;

public class kryssord {
	public static void main(String[] args) {
	try {
	Scanner fra_fil = new Scanner (new File("kryssordfil.txt"));
	
	int ant_kol = fra_fil.nextInt();
	int ant_rad = fra_fil.nextInt();
	int kryss[][] = new int [ant_kol + 2][ant_rad + 2];
	
	for (int i=0; i<ant_rad+1; i++)
		for (int j=0; j<ant_kol+1; j++)
			kryss[j][i] = -1;
			
	for (int i=1; i<=ant_rad; i++)
		for (int j=0; j<=ant_kol; j++)
			kryss[j][i] = 0;
			
	for (int i=0; i<=ant_rad+1; i++) {
		for(int j=0; j<=ant_kol+1; j++)
			System.out.printf("%3d", kryss[j][i]);
		System.out.println();
		}
	} catch(FileNotFoundException bla) {
    throw new FileNotFoundException(bla);
    }
	}
}

Actually it would be added to main(). However, your class design isn’t really there. I suggest splitting your code into another method and calling that method from main. Example:


import java.io.*;
import java.text.*;
import java.util.Scanner;

public class kryssord {

public boolean doStuff () throws FileNotFoundException
{
try {
	Scanner fra_fil = new Scanner (new File("kryssordfil.txt"));
	
	int ant_kol = fra_fil.nextInt();
	int ant_rad = fra_fil.nextInt();
	int kryss[][] = new int [ant_kol + 2][ant_rad + 2];
	
	for (int i=0; i<ant_rad+1; i++)
		for (int j=0; j<ant_kol+1; j++)
			kryss[j][i] = -1;
			
	for (int i=1; i<=ant_rad; i++)
		for (int j=0; j<=ant_kol; j++)
			kryss[j][i] = 0;
			
	for (int i=0; i<=ant_rad+1; i++) {
		for(int j=0; j<=ant_kol+1; j++)
			System.out.printf("%3d", kryss[j][i]);
		System.out.println();
		}
	} catch(FileNotFoundException bla) {
    throw new FileNotFoundException(bla);
    }
   return true;
}
	public static void main(String[] args) {
	  doStuff();
	}
}

This can be done in an even better way than this, but it gives you an idea of where you should be going :slight_smile:

Okay thanks, I think I’m beginning to grasp the concept of this - Thanks a lot for the help!

Just another note on this. You dont have to throw the FileNotFoundException … its already been thrown. So you can do this:

try {
  Scanner fra_fil = new Scanner (new File("kryssordfil.txt"));
  }
catch (FileNotFoundException fnfe) {
  // code to handle the file not being found
  }

Also one very common mistake that I see with beginners is using a try / catch AND declaring the method throws the exception … nope, nope, nope. You do one or the either UNLESS you are double throwing the exception (as you are here). Personally I will not throw an exception from an exception catch as I want to be able to handle the exception as quickly (and locally as possible)

Also remember you can catch an exception like this with one of it’s super classes. For a FileNotFound you could catch (or throw) a java.io.IOException. This would be the case if you really didnt have a plan to actually do anything if the exception occurs (which does happen). But that being said it really bothers me when I see people doing this:

try {
 // some code
  }
catch (Exception e){

  }

Unless you truely dont give a damn what kind of exception occured and dont plan to try to handle the different type exceptions you could have this but its a REAL BAD idea.

Also remember you can multiple catchs to one try (but never the other way around) so you can do this:

try {

 }
catch (java.io.FileNotFoundException fnfe) {

 }
catch (NullPointerException) {

 }
catch (java.sql.SQLException sqle) {

 }
catch (java.io.IOException ioe) {

 }

Now this is an extreme example but you get the point. Notice I am not only catch the file not found but also the IO Exception at the end, which again is the superclass of fnf

Off Topic:

I think everyone uses the same dumb exception names :wink:

dc dalton: thanks for those suggestion, it cleared up even more for me - I guess I was trying to “double throw” the exception, and that was what caused the troubles.

Also I was wondering if it is possible for me to include the filename specified in the Scanner object I created in my error message, just to make the error message a bit clearer.

Off Topic:

I think you’re right Vinnie, and it can be confusing for for us n00bs :stuck_out_tongue: