Would Any One Happen To Know.....

If There's A Possible Form Of Exception Handling For This Program Besides Exception(ex. NumberFormatException, ArithmaticException, & etc.)

******************************************************************************

Here's The main() class

******************************************************************************

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

publicclass UseBookFinalextends JFrameimplements ActionListener

{

JLabel nonFictionLabel =new JLabel("Enter The Name Of A Non-Fictional Book");

JLabel fictionLabel =new JLabel("Enter The Name Of A Fictional Book");

JTextField nonFictionName =new JTextField(40);

JTextField fictionName =new JTextField(40);

JButton OK =new JButton("OK");

JTextField nonFictionResult =new JTextField(40);

JTextField fictionResult =new JTextField(40);

public UseBookFinal()

{

super("Please Select A Book");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel pane =new JPanel();

pane.add(nonFictionLabel);

pane.add(nonFictionName);

pane.add(fictionLabel);

pane.add(fictionName);

pane.add(OK);

pane.add(nonFictionResult);

pane.add(fictionResult);

setContentPane(pane);

OK.addActionListener(this);

}

publicstaticvoid main(String[] args)

{

JFrame aFrame =new UseBookFinal();

aFrame.setSize(500,250);

aFrame.setVisible(true);

}

publicvoid actionPerformed(ActionEvent e)

{

NonFictionFinal aBook =new NonFictionFinal();

FictionFinal anotherBook =new FictionFinal();

String nonFictionTitle;

String fictionTitle;

try

{

nonFictionTitle = nonFictionName.getText();

aBook.setTitle(nonFictionTitle);

fictionTitle = fictionName.getText();

anotherBook.setTitle(fictionTitle);

}

catch(Exception error)

{

nonFictionResult.setText("Invalid Selection");

fictionResult.setText("Invalid Selection");

}

nonFictionResult.setText(aBook.getTitle() +" Is A Non-Fictional Book That Costs " + aBook.getPrice());

fictionResult.setText(anotherBook.getTitle() +" Is A Fictional Book That Costs " + anotherBook.getPrice());

}

}

******************************************************************************

Here's The Parent Class

******************************************************************************

publicabstractclass BookFinal

{

private String title;

protecteddouble price;

public BookFinal()

{

setPrice();

}

publicvoid setTitle(String name)

{

title = name;

}

public String getTitle()

{

return title;

}

publicdouble getPrice()

{

return price;

}

publicabstractvoid setPrice();

}

******************************************************************************

Here Child Class #1

******************************************************************************

publicclass FictionFinalextends BookFinal

{

public FictionFinal()

{

super();

}

publicvoid setPrice()

{

price = 24.99;

}

}

******************************************************************************

Here's Child Class #2

******************************************************************************

publicclass NonFictionFinalextends BookFinal

{

public NonFictionFinal()

{

super();

}

publicvoid setPrice()

{

price = 37.99;

}

}

[6865 byte] By [CyberSafya] at [2007-10-2 8:09:51]
# 1

It appears that no Exception is thrown in the code you posted, so I guess your question has to do with validating the user input. That is, the code inside the try {} somehow has to decide whether the user input is valid and if it is not, you want the code in the catch(){} to execute.

Is that right?

If it is, then you need to tell us what makes the user input valid and/or what makes it invalid.

atmguya at 2007-7-16 22:05:19 > top of Java-index,Developer Tools,Java Compiler...