Confused about IOExceptions

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.List;

import java.util.Random;

publicclass Genster{

List<String> wordList_B = initWordList_B();//I get the error for this line

privatestatic List<String> initWordList_B()throws IOException{

List<String> wordList_B =new ArrayList<String>();

InputStream stream = Genster.class.getResourceAsStream("O.RLY");

BufferedReader in =new BufferedReader(new InputStreamReader(stream));

String word = in.readLine();

while(word !=null){

wordList_B.add(word);

word = in.readLine();

}

return wordList_B;

}

}

Error: /home/kavon/Java Projects/HardPw/src/hardpw/Genster.java:12: unreported exception java.io.IOException; must be caught or declared to be thrown

How do I declare the exception? I know I already have one in the initWordList method, but that is the only part of that method I don't understand... and I had gotten it from a helper in this forum.

[2042 byte] By [kavon89a] at [2007-11-27 10:52:53]
# 1

> import java.io.BufferedReader;

> import java.io.IOException;

> import java.io.InputStream;

> import java.io.InputStreamReader;

> import java.util.ArrayList;

> import java.util.LinkedList;

> import java.util.List;

> import java.util.Random;

>

> public class Genster {

>

> List<String> wordList_B = initWordList_B(); //I get

> the error for this line

>

> private static List<String> initWordList_B() throws

> IOException {

> List<String> wordList_B = new

> ArrayList<String>();

> InputStream stream =

> Genster.class.getResourceAsStream("O.RLY");

> BufferedReader in = new BufferedReader(new

> InputStreamReader(stream));

>

> String word = in.readLine();

>while(word != null) {

>wordList_B.add(word);

>word = in.readLine();

> }

>return wordList_B;

> }

>

> Error: /home/kavon/Java

> Projects/HardPw/src/hardpw/Genster.java:12:

> unreported exception java.io.IOException; must be

> caught or declared to be thrown

>

> How do I declare the exception? I know I already have

> one in the initWordList method, but that is the only

> part of that method I don't understand... and I had

> gotten it from a helper in this forum.

When you add "throws Exception" to a method's signature, all you're saying is that you don't want that method to handle that particular exception. You are delegating that responsibility up the call stack... which means when you call this function, you must either:

a) throw the IOException again

b) handle the exception in a try/catch/finally block.

Navy_Codera at 2007-7-29 11:40:32 > top of Java-index,Java Essentials,New To Java...
# 2

but if you want to try/catch it, I think you need to do that within a method block (the class constructor perhaps?).

petes1234a at 2007-7-29 11:40:32 > top of Java-index,Java Essentials,New To Java...
# 3

I want to throw the IO again.

Where do I put the O.RLY file by the way. Should I put it next to the compiled .jar or in the src -> hardpw folder?

kavon89a at 2007-7-29 11:40:32 > top of Java-index,Java Essentials,New To Java...
# 4

If you want to throw the exception, just don't catch it.

The file has to be in the JAR file, in the same directory as Genster.class.

ejpa at 2007-7-29 11:40:32 > top of Java-index,Java Essentials,New To Java...
# 5

Hi Kavon,

I guess, your code can be like this. And this way, it takes care of the exception and will not trouble the flow of your programme (that's what we expect by exception handling ) also will save the results in to a text file and save it in your pc.

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Vector;

public class FileUtil {

DataOutputStream dos;

/*

* Utility method to write a given text to a file

*/

public boolean writeToFile(String fileName, String dataLine,

boolean isAppendMode, boolean isNewLine) {

if (isNewLine) {

dataLine = "\n" + dataLine;

}

try {

File outFile = new File(fileName);

if (isAppendMode) {

dos = new DataOutputStream(new FileOutputStream(fileName, true));

} else {

dos = new DataOutputStream(new FileOutputStream(outFile));

}

dos.writeBytes(dataLine);

dos.close();

} catch (FileNotFoundException ex) {

return (false);

} catch (IOException ex) {

return (false);

}

return (true);

}

/*

* Reads data from a given file

*/

public String readFromFile(String fileName) {

String DataLine = "";

try {

File inFile = new File(fileName);

BufferedReader br = new BufferedReader(new InputStreamReader(

new FileInputStream(inFile)));

DataLine = br.readLine();

br.close();

} catch (FileNotFoundException ex) {

return (null);

} catch (IOException ex) {

return (null);

}

return (DataLine);

}

public boolean isFileExists(String fileName) {

File file = new File(fileName);

return file.exists();

}

public boolean deleteFile(String fileName) {

File file = new File(fileName);

return file.delete();

}

/*

* Reads data from a given file into a Vector

*/

public Vector fileToVector(String fileName) {

Vector v = new Vector();

String inputLine;

try {

File inFile = new File(fileName);

BufferedReader br = new BufferedReader(new InputStreamReader(

new FileInputStream(inFile)));

while ((inputLine = br.readLine()) != null) {

v.addElement(inputLine.trim());

}

br.close();

} // Try

catch (FileNotFoundException ex) {

//

} catch (IOException ex) {

//

}

return (v);

}

/*

* Writes data from an input vector to a given file

*/

public void vectorToFile(Vector v, String fileName) {

for (int i = 0; i < v.size(); i++) {

writeToFile(fileName, (String) v.elementAt(i), true, true);

}

}

/*

* Copies unique rows from a source file to a destination file

*/

public void copyUniqueElements(String sourceFile, String resultFile) {

Vector v = fileToVector(sourceFile);

v = MiscUtil.removeDuplicates(v);

vectorToFile(v, resultFile);

}

} // end FileUtil

class MiscUtil {

public static boolean hasDuplicates(Vector v) {

int i = 0;

int j = 0;

boolean duplicates = false;

for (i = 0; i < v.size() - 1; i++) {

for (j = (i + 1); j < v.size(); j++) {

if (v.elementAt(i).toString().equalsIgnoreCase(

v.elementAt(j).toString())) {

duplicates = true;

}

}

}

return duplicates;

}

public static Vector removeDuplicates(Vector s) {

int i = 0;

int j = 0;

boolean duplicates = false;

Vector v = new Vector();

for (i = 0; i < s.size(); i++) {

duplicates = false;

for (j = (i + 1); j < s.size(); j++) {

if (s.elementAt(i).toString().equalsIgnoreCase(

s.elementAt(j).toString())) {

duplicates = true;

}

}

if (duplicates == false) {

v.addElement(s.elementAt(i).toString().trim());

}

}

return v;

}

public static Vector removeDuplicateDomains(Vector s) {

int i = 0;

int j = 0;

boolean duplicates = false;

String str1 = "";

String str2 = "";

Vector v = new Vector();

for (i = 0; i < s.size(); i++) {

duplicates = false;

for (j = (i + 1); j < s.size(); j++) {

str1 = "";

str2 = "";

str1 = s.elementAt(i).toString().trim();

str2 = s.elementAt(j).toString().trim();

if (str1.indexOf('@') > -1) {

str1 = str1.substring(str1.indexOf('@'));

}

if (str2.indexOf('@') > -1) {

str2 = str2.substring(str2.indexOf('@'));

}

if (str1.equalsIgnoreCase(str2)) {

duplicates = true;

}

}

if (duplicates == false) {

v.addElement(s.elementAt(i).toString().trim());

}

}

return v;

}

public static boolean areVectorsEqual(Vector a, Vector b) {

if (a.size() != b.size()) {

return false;

}

int i = 0;

int vectorSize = a.size();

boolean identical = true;

for (i = 0; i < vectorSize; i++) {

if (!(a.elementAt(i).toString().equalsIgnoreCase(b.elementAt(i)

.toString()))) {

identical = false;

}

}

return identical;

}

public static Vector removeDuplicates(Vector a, Vector b) {

int i = 0;

int j = 0;

boolean present = true;

Vector v = new Vector();

for (i = 0; i < a.size(); i++) {

present = false;

for (j = 0; j < b.size(); j++) {

if (a.elementAt(i).toString().equalsIgnoreCase(

b.elementAt(j).toString())) {

present = true;

}

}

if (!(present)) {

v.addElement(a.elementAt(i));

}

}

return v;

}

}// end of class

O.RLY file can be located as I've showed in the code and the best way to give path or put that file in the same folder as this class exists =)

hope this helps you.

Message was edited by:

ArchiEnger.711

ArchiEnger.711a at 2007-7-29 11:40:32 > top of Java-index,Java Essentials,New To Java...
# 6

ArchiEnger.711, I honestly have no idea what to do with the code you posted. I get a "unchecked or unsafe operations" error when I just tried it for fun.

I am really just looking to throw the IOException again, I'm not worried about dealing with the exception if it can't find it because the files will be bundled with the .jar file.

The problem I'm having is actually throwing the exception again. I don't know the proper format for doing this for that one line giving me an error. I've tried all sorts of combinations but they haven't worked. I just don't understand how the throws keyword works in syntax.

kavon89a at 2007-7-29 11:40:32 > top of Java-index,Java Essentials,New To Java...
# 7

All you need is this:

public class Genster {

List<String> wordList_B;

// Add a constructor

public Genster() throws IOException

{

this.wordList_B = initWordList_B();

}

// etc

}

ejpa at 2007-7-29 11:40:32 > top of Java-index,Java Essentials,New To Java...