Exception Handlers in java............


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Exception Handlers in java............

  1. #1
    Join Date
    Apr 2007
    Location
    Sunnyvale
    Posts
    2

    Exception Handlers in java............

    Iam trying to write the input from text file into an automotive object.

    public void readFile(Automotive a1)
    {

    BufferedReader inputstream=null;
    BufferedWriter outputstream=null;
    StringTokenizer st;

    try
    {
    inputstream = new BufferedReader(new FileReader("C:\\CarInputData.txt"));

    outputstream = new BufferedWriter(new FileWriter("C:\\CarOutputData.txt"));
    String line= inputstream.readLine();

    st = new StringTokenizer(line,":");
    a1.setName(st.nextToken());
    outputstream.write("Car Model :" + a1.getName());
    outputstream.newLine();
    outputstream.newLine();

    catch(NullPointerException e)
    {
    System.out.println("Null Pointer Exception ");
    }

    catch(NumberFormatException nfe)
    {
    System.out.println("Cannot read the number ");
    }

    catch(ArrayIndexOutOfBoundsException e)
    {
    System.out.println("Array out of Bounds Exception");
    }

    catch (FileNotFoundException e)
    {
    System.out.println("File not found!");
    try
    {
    inputstream.close();
    outputstream.close();
    }
    catch (IOException ioe) {} // disregard close failure
    }
    catch (IOException e)
    {
    e.printStackTrace();
    try
    {
    inputstream.close();
    outputstream.close();
    }
    catch (IOException ioe) {} // disregard close failure
    System.out.println("Unable to read file!");
    }

    }// end of readFile

    -------------------------------------------------------------------
    I have the following requirements:

    1.Create an exception handler so it handles at least 5 exceptions
    2.(Optional) Enhance your design and code to create any abstract classes so the code is extensible and reusable.

    Nothing more is mentioned about them.....

    Is my analysis to create a custom exception right???

  2. #2
    Join Date
    Jan 2006
    Location
    Lourdes, Lubao, Pampanga, Philippines
    Posts
    36

    reply

    here's a code that i've made:

    // CAutomotive.java
    // 2007-May-10

    package automotive;

    import java.awt.*;
    import javax.swing.*;
    import java.io.*;

    public final class CAutomotive {

    // attribute(s)
    private JFrame jframe;
    private JComboBox jcboList;

    // constructor(s)
    public CAutomotive(String title) {

    jframe.setDefaultLookAndFeelDecorated( true );
    jframe = new JFrame( title );
    jcboList = new JComboBox();

    } // close CAutomotive(String)

    // accessor(s)
    public void setProperties() {

    jcboList.setBackground( Color.WHITE );

    try {

    BufferedReader input =
    new BufferedReader( new FileReader("C:\\automotive\\cls\\car_input_data.txt") );
    String s = "";

    while( (s = input.readLine() ) != null ) {
    jcboList.addItem( s );
    } // close while

    } catch (IOException e) {

    } // close try catch

    } // close setProperties()

    // other method(s)
    public void launchFrame() {

    setProperties();

    jframe.getContentPane().add( jcboList, BorderLayout.NORTH );
    jframe.setSize( 200, 200 );
    jframe.setResizable( false );
    jframe.setVisible( true );


    } // close launchFrame()

    } // close CAutomotive class


    why do you need to have a lot of exceptions?....
    what is the purpose of your Automotive object?...
    can you explain more so I can help you?...

Similar Threads

  1. Java book
    By Lou in forum Java
    Replies: 9
    Last Post: 09-19-2007, 06:58 AM
  2. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 07:46 AM
  3. Exception Handling in Exception Handlers
    By David Williams in forum .NET
    Replies: 1
    Last Post: 05-28-2002, 03:19 PM
  4. Re: VB vs. Visual Age for Java
    By JJ in forum Enterprise
    Replies: 1
    Last Post: 07-06-2000, 05:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center