Why am I getting an error message “unreported exception Java.io in a java code when trying to read from a text file”?

Every moethod in java must declare the exceptions it might throw except RuntimeException s. Since your method throws FileNotFoundException it must do one of 2 things.

Every moethod in java must declare the exceptions it might throw except RuntimeExceptions. Since your method throws FileNotFoundException, it must do one of 2 things: declare it as throws FileNotFoundException use a try,catch block, and handle this exception. (*)The above is referring to the activation of your constructor, i.e.

The method that is actually activating the c'tor.

You should probably surround your use of the readTextFile1() method within a try-catch block: readTextFile1 cls = new readTextFile1(); try { cls. ReadTextFile1(); } catch (FileNotFoundException e) { // report error or something } Additionally your class is misnamed as it doesn't describe its use; it appears to store a list of Employee objects and reads that data using this method. You would do better to call the class DepartmentReader or something (note the leading uppercase letter).

If exceptions are possible in your code, you have two options: Catch them (and optionally handle them) or rethrow them. Catching them is done by blocks starting with try{} and ending in catch(Exceptiontype exceptionname) (with optional finally{} block). Throwing them further up means that you let the calling stack further up handle the exception.

For this purpose, you defined your class with the phrase "throws FileNotFoundException". This means that from now on, using this method in any code will need to acknowledge this optional exception, again either by catching it or throwing it further up still. So whenever you use the method above, you need to do one of the two mentioned things, and I'm sure you missed both.

You might want to read up on exceptions and exception handling. Public readTextFile1() throws FileNotFoundException This declares that readTextFile1 can throw FileNotFoundException, which is a so-called checked exception. That means that, at the point you call this method, you must either catch it, or explicitly declare that the calling method can also throw this.

Catching it would work thusly: readTextFile1 file = new readTextFile1(); try { readTextFile1. ReadTextFile1(); } catch (FileNotFoundException ex) { // Here, handle the case that the file is not found. For example: System.err.

Println("File not found"); } (By the way, class names in Java are usually written with a capital letter, and should be in the form of a noun, not a verb. For example, TextFileReader. ).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions