java101: Catching and Handling Exceptions question
Dear all,
I am learning java from http://java.sun.com/docs/books/tutorial/essential/exceptions/putItTogether.html
However, I think the code in ListOfNumbers may have some problem, I put void in front of the function, but still have error. Could you please tell me any problem in the program?
thanks
import java.util.Vector;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.*;
publicclass Main{
private Vector vector;
privateint SIZE = 10;
publicstaticvoid main(String[] args)throws IOException{
public ListOfNumbers (){
vector =new Vector(SIZE);
for (int i = 0; i < SIZE; i++){
vector.addElement(new Integer(i));
}
}
publicvoid writeList(){
PrintWriter out =new PrintWriter(
new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++){
out.println("Value at: " + i +" = " +
vector.elementAt(i));
}
out.close();
}
FileInputStream in =null;
FileOutputStream out =null;
in =new FileInputStream("xanadu.txt");
out =new FileOutputStream("outagain.txt");
int c;
while ((c = in.read()) != -1){
out.write(c);
}
}
publicvoid writeList(){
PrintWriter out =null;
try{
System.out.println("Entering try statement");
out =new PrintWriter(
new FileWriter("OutFile.txt"));
for (int i = 0; i < 5; i++)
out.println("Value at: " + i +" = "
+ vector.elementAt(i));
}catch (ArrayIndexOutOfBoundsException e){
System.err.println("Caught "
+"ArrayIndexOutOfBoundsException: "
+e.getMessage());
}catch (IOException e){
System.err.println("Caught IOException: "
+ e.getMessage());
}finally{
if (out !=null){
System.out.println("Closing PrintWriter");
out.close();
}
else{
System.out.println("PrintWriter not open");
}
}
}
}
[4605 byte] By [
marco_wua] at [2007-11-27 8:11:07]

I have also post the error message here, I change the name of the file and the class name. I think I can not handle the error message #1 and #2. Please help me! Thanks
/*
* Main.java
*
* Created on June 19, 2007, 10:54 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package bytestreams01;
/**
*
* @author Marco
*/
import java.util.Vector;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.*;
public class Main {
private Vector vector;
private int SIZE = 10;
public static void ListOfNumbers(String[] args) throws IOException {
public ListOfNumbers () {
vector = new Vector(SIZE);
for (int i = 0; i < SIZE; i++) {
vector.addElement(new Integer(i));
}
}
FileInputStream in = null;
FileOutputStream out = null;
in = new FileInputStream("xanadu.txt");
out = new FileOutputStream("outagain.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
}
public void writeList() {
PrintWriter out = null;
try {
System.out.println("Entering try statement");
out = new PrintWriter(
new FileWriter("OutFile.txt"));
for (int i = 0; i < 5; i++)
out.println("Value at: " + i + " = "
+ vector.elementAt(i));
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Caught "
+ "ArrayIndexOutOfBoundsException: "
+e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException: "
+ e.getMessage());
} finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
}
else {
System.out.println("PrintWriter not open");
}
}
}
}
error message
init:
deps-jar:
Compiling 1 source file to C:\Users\Marco\bytestreams01\build\classes
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:29: illegal start of expression
public ListOfNumbers () {
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:29: invalid method declaration; return type required
public ListOfNumbers () {
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:41: <identifier> expected
in = new FileInputStream("xanadu.txt");
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:42: <identifier> expected
out = new FileOutputStream("outagain.txt");
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:45: illegal start of type
while ((c = in.read()) != -1) {
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:45: <identifier> expected
while ((c = in.read()) != -1) {
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:45: ';' expected
while ((c = in.read()) != -1) {
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:50: class, interface, or enum expected
public void writeList() {
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:53: class, interface, or enum expected
try {
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:55: class, interface, or enum expected
out = new PrintWriter(
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:57: class, interface, or enum expected
for (int i = 0; i < 5; i++)
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:57: class, interface, or enum expected
for (int i = 0; i < 5; i++)
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:57: class, interface, or enum expected
for (int i = 0; i < 5; i++)
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:61: class, interface, or enum expected
} catch (ArrayIndexOutOfBoundsException e) {
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:66: class, interface, or enum expected
} catch (IOException e) {
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:70: class, interface, or enum expected
} finally {
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:73: class, interface, or enum expected
out.close();
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:75: class, interface, or enum expected
}
C:\Users\Marco\bytestreams01\src\bytestreams01\ListOfNumbers.java:78: class, interface, or enum expected
}
19 errors
BUILD FAILED (total time: 0 seconds)