Total members 11346 |It is currently Fri Aug 01, 2014 11:27 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

All times are UTC [ DST ]



  Exception Handling in a project
Tue Jun 02, 2009 5:52 am 

hi good morning everybody

I am doing a java project,I want to handle all the exception in a single
class,wherever the exception raises ,I want to handle it in a class
can anyone send some sample for this ,


Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time
  Re: Exception Handling in a project
Fri Jun 19, 2009 3:49 am 
Even it's just one class, you still need to use interface for that. Each interface will have its own listener class which will override all the methods. This is one sample code that you could try to analyze. Hope it's helpful :)
Code:
//problem statement 5.P.1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//import java.awt.Container;

public class MyDealer extends JApplet implements KeyListener
{
   //variable for the panel
   JPanel panelObject;

   //variables for labels
   JLabel labelCustName;
   JLabel labelCustCellNo;
   JLabel labelCustAddress;
   JLabel labelCustAge;

   //variables for data entry controls
   JTextField textCustName;
   JTextField textCustCellNo;
   JTextField textAddress;
   JTextField textCustAge;

   String name, no, add, age;

   JButton btnSubmit;

   //variables for the layout
   GridBagLayout gbObject;
   GridBagConstraints gbc;

   public void init()
   {
      //initializes the layout variables
      gbObject = new GridBagLayout();
      gbc = new GridBagConstraints();
      panelObject = (JPanel)getContentPane();
      panelObject.setLayout(gbObject);

      setSize(800,200);

      //initializes label controls
      labelCustName = new JLabel("Dealer Name");
      labelCustCellNo = new JLabel("Cell Number ");
      labelCustAddress= new JLabel("Address");
      labelCustAge = new JLabel("Age");

      //initializes data entry controls
      textCustName = new JTextField(30);
      textCustCellNo = new JTextField(15);
      textCustAge = new JTextField(2);
      textAddress = new JTextField(45);
      btnSubmit = new JButton("SUBMIT");


      //Add listeners to the controls
      textCustName.addKeyListener(this);
      textCustCellNo.addKeyListener(this);
      textAddress.addKeyListener(this);
      textCustAge.addKeyListener(this);

      //add controls for customer name;
      gbc.anchor = GridBagConstraints.NORTHWEST;
      gbc.gridx = 1;
      gbc.gridy = 5;
      gbObject.setConstraints(labelCustName,gbc);
      panelObject.add(labelCustName);

      gbc.anchor = GridBagConstraints.NORTHWEST;
      gbc.gridx = 4;
      gbc.gridy = 5;
      gbObject.setConstraints(textCustName,gbc);
      panelObject.add(textCustName);

      //add controls for cell number
      gbc.anchor = GridBagConstraints.NORTHWEST;
      gbc.gridx =1;
      gbc.gridy =8;
      gbObject.setConstraints(labelCustCellNo,gbc);
      panelObject.add(labelCustCellNo);

      gbc.anchor = GridBagConstraints.NORTHWEST;
      gbc.gridx =4;
      gbc.gridy =8;
      gbObject.setConstraints(textCustCellNo, gbc);
      panelObject.add(textCustCellNo);

      //add controls for package
      gbc.anchor = GridBagConstraints.NORTHWEST;
      gbc.gridx =1;
      gbc.gridy =11;
      gbObject.setConstraints(labelCustAddress, gbc);
      panelObject.add(labelCustAddress);

      gbc.anchor = GridBagConstraints.NORTHWEST;
      gbc.gridx =4;
      gbc.gridy =11;
      gbObject.setConstraints(textAddress, gbc);
      panelObject.add(textAddress);

      //add controls for customer age
      gbc.anchor = GridBagConstraints.NORTHWEST;
      gbc.gridx =1;
      gbc.gridy =14;
      gbObject.setConstraints(labelCustAge, gbc);
      panelObject.add(labelCustAge);

      gbc.anchor = GridBagConstraints.NORTHWEST;
      gbc.gridx =4;
      gbc.gridy =14;
      gbObject.setConstraints(textCustAge, gbc);
      panelObject.add(textCustAge);

      gbc.anchor = GridBagConstraints.SOUTH;
      gbc.gridx = 4;
      gbc.gridy = 16;
      gbObject.setConstraints(btnSubmit,gbc);
      panelObject.add(btnSubmit);


   }

   // BEGINNING OF KEY LISTENER FUNCTIONS
   public void keyPressed(KeyEvent e)
   {
      Object obj = e.getSource();
         if (obj == textCustName)
         {
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_ENTER)
            {
               String name = textCustName.getText();
               if (name.length() == 0)
               {
                  JOptionPane.showMessageDialog(this,"Name cannot be empty");
                  //showStatus("cannot be empty");
                  //return;
               }
               else
               {
                  textCustCellNo.requestFocusInWindow();
               }
            }
         }

         if (obj == textCustCellNo)
         {
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_ENTER)
            {
               String no = textCustCellNo.getText();
               if (no.length() != 10)
               {
                  JOptionPane.showMessageDialog(this,"Mobile number entered should be of ten digits");
               }
               else
               {
                  textAddress.requestFocusInWindow();
               }
            }
         }

         if (obj == textAddress)
         {
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_ENTER)
            {
                  textCustAge.requestFocusInWindow();
            }
         }

         if (obj == textCustAge)
         {
            int key = e.getKeyCode();
            if (key == KeyEvent.VK_ENTER)
            {
               int age = Integer.parseInt(textCustAge.getText());
               if (age <= 18 || age >= 100)
               {
                  JOptionPane.showMessageDialog(this,"Wrong Age Input. Please Recheck!");
               }
               else
               {
                  btnSubmit.requestFocusInWindow();
               }
            }
         }
   }
   public void keyReleased(KeyEvent e)
   {
   }
   public void keyTyped(KeyEvent e)
   {


      //showStatus(" Recently typed characters are : " + e.getKeyChar());
   }
   //END OF KEY LISTENER FUNCTIONS
}




Author:
Newbie
User avatar Posts: 16
Have thanks: 0 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Exception Handling in a project
 how to design a exception handling for a project     -  
 Exception handling     -  
 exception handling try and catch in Python     -  
 Out of Bounds exception     -  
 Exception is not clear     -  
 Multiple Exception Catching     -  
 Exception Handler in java     -  
 Define your own exception class     -  
 String Too Long Exception     -  
 Catch database connection exception     -  

All times are UTC [ DST ]









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team