Results 1 to 6 of 6
  1. #1
    carman12 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default 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....

  2. #2
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    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.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    Yep.
    You need to actually write the EmptyLineException.java code.

    I will also point out that this is a bad habit to get into:
    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");	   
    		}
    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.

  4. #4
    carman12 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    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.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    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:
    Java Code:
    new MySpecialException("Oh no. what went wrong?");
    it will create an exception of your class.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

    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
    }
    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

Similar Threads

  1. Help with Catching Exceptions
    By javaman1 in forum New To Java
    Replies: 4
    Last Post: 09-11-2010, 02:06 AM
  2. Exceptions
    By Nerijus in forum New To Java
    Replies: 8
    Last Post: 05-18-2010, 01:44 PM
  3. Exceptions & More
    By besweeet in forum New To Java
    Replies: 12
    Last Post: 04-29-2010, 09:06 PM
  4. Exceptions
    By hedonist in forum New To Java
    Replies: 10
    Last Post: 09-08-2009, 08:38 AM
  5. Need Help With Exceptions
    By maggie_2 in forum New To Java
    Replies: 5
    Last Post: 12-15-2008, 07:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •