Results 1 to 6 of 6
Thread: Creating new exceptions.
- 03-01-2011, 02:48 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
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;
Java 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....
- 03-01-2011, 02:54 AM #2
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
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.
- 03-01-2011, 08:53 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Yep.
You need to actually write the EmptyLineException.java code.
I will also point out that this is a bad habit to get into:
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.Java 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"); }
- 03-02-2011, 12:52 AM #4
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
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.
- 03-02-2011, 12:56 AM #5
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:
it will create an exception of your class.Java Code:new MySpecialException("Oh no. what went wrong?");
- 03-02-2011, 12:56 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
I strongly suggest reading the sun tutorials on Exceptions, they do a pretty good job of explaining it.Java Code:class MyException extends Exception{ //create your exception..constructors, variables, etc. //You can have pretty much anything a regular class has in your exception }
Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
Also check out
Exceptions in Java - JavaWorld
Similar Threads
-
Help with Catching Exceptions
By javaman1 in forum New To JavaReplies: 4Last Post: 09-11-2010, 02:06 AM -
Exceptions
By Nerijus in forum New To JavaReplies: 8Last Post: 05-18-2010, 01:44 PM -
Exceptions & More
By besweeet in forum New To JavaReplies: 12Last Post: 04-29-2010, 09:06 PM -
Exceptions
By hedonist in forum New To JavaReplies: 10Last Post: 09-08-2009, 08:38 AM -
Need Help With Exceptions
By maggie_2 in forum New To JavaReplies: 5Last Post: 12-15-2008, 07:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks