-
Creating new exceptions.
Hello All.
I'm stuck but then again I guess that's why i'm here.
I'm here trying to create a new exception in a try catch but I keep getting and error;
Code:
try{
//open module file to obtain statically stored
//module names
FileInputStream fstream = new FileInputStream("module2.txt");
//convert module file to computer language with
//data stream reader
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = br.readLine();
//Read File Line By Line
while (strLine != null){
// Print the content on the console
if (strLine.equals("")){
throw new EmptyLineException();
}
System.out.println(strLine);
imod = new InstructionModule(strLine);
modulevector.add(imod);//storage of module name and number
strLine = br.readLine();
}
//Close the input stream
in.close();
}//end try
catch (EOFException eof){//Catch exception if any
System.err.println("Error: " + eof.getMessage());
}
catch (EmptyLineException e){
System.out.println("The line read from module2 was empty!");
}
catch (Exception e){//Catch exception if any
System.err.println("Error: I cannot find my file");
}
OUTPUT
(classname).java:75: cannot find symbol
symbol : class EmptyLineException
location: class (classname) -------> classname refers name of classs file
throw new EmptyLineException();
I read online and I keep seeing this as the way to create a new exception but no matter what I do I can't get away from this error.
PLEASE PLEASE HELP ME....
-
Yes, that's how you create a new instance of an EmptyLineException, but you haven't yet created the definition (i.e class file) for the EmptyLineException. Remember, the compiler has to know how to make something (that's what the class definition is for) as well as how to create new instances (objects) of that something.
-
Yep.
You need to actually write the EmptyLineException.java code.
I will also point out that this is a bad habit to get into:
Code:
catch (EOFException eof){//Catch exception if any
System.err.println("Error: " + eof.getMessage());
}
catch (EmptyLineException e){
System.out.println("The line read from module2 was empty!");
}
catch (Exception e){//Catch exception if any
System.err.println("Error: I cannot find my file");
}
You should get used to using <exception>.printStatckTrace() otherwise you'll lose lots of error information. That information (the stack trace) is the sort of thing that gets asked for int hese sorts of forums should you hit a problem when running your code.
-
OK I think i understand what you guys are saying but I have no experience with creating class exceptions.
This is the first time i'm being introduced to exceptions. I read online where they sai to create the exception in a new class and instantiate the class in the file i want the exception then use is.
Is this what you are referring too.
-
It would seem that you are getting hung up on the word "exception". They are very similar to objects. You write a class with instance variables and methods if needed. Then when you have:
Code:
new MySpecialException("Oh no. what went wrong?");
it will create an exception of your class.
-
An exception is a class, there is really no difference, they inherit from Exception. So in another file(or the same file) you would create a new exception like this
Code:
class MyException extends Exception{
//create your exception..constructors, variables, etc.
//You can have pretty much anything a regular class has in your exception
}
I strongly suggest reading the sun tutorials on Exceptions, they do a pretty good job of explaining it.
Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
Also check out
Exceptions in Java - JavaWorld