Results 1 to 5 of 5

Thread: Code for Exception in Java

  1. #1
    Join Date
    Aug 2006
    Posts
    155

    Code for Exception in Java

    Can someone help me to write a code for an exception in Java. No problem if there is no detailed coding about the exception, just few lines will be OK, so I can understand the logic of it. Lets assume that I have got my main function. In that I am asking the user to input their First Name. Now I want an exception saying that "You have to enter the name" or "You have entered a number as your name, type properly" when the user has entered nothing or the user has entered the number in the Name field. Also can I handle the exceptions in the other class.?? So that it gets triggered when such scene occurs. Can someone help me..??
    Desktop * Athlon X2 4200 | 2048M RAM | 160G HD | 7600GT
    MacMini * G4 1.33GHz | 512M RAM | 40G HD | Radeon 9200
    Laptop * Sempron 2800 | 512M RAM | 60G HD
    PDA * Dell Axim x51 | 128M Internal | 256M SD Card

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Code for Exception in Java

    The event that occurs during the execution of a program, which disrupts the normal flow of the program's instructions is known as an Exception. The method creates an object and hands it off to the runtime system when an error occurs within a method. The object that contains information about the error, including its type and the state of the program when the error occurred is called as exception object. Handling an exception to the runtime system is called as throwing an exception.

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: Code for Exception in Java

    If the Java program is valid then it should honor the Catch or Specify Requirement. Then only it can called as Valid Java programming language's code. The exceptions must be enclosed by either of the following if the code that might throw :
    A try statement that catches the exception.
    • As described in Catching and Handling Exceptions, the try must provide a handler for the exception.
    • A method that specifies that it can throw the exception. Also as described in Specifying the Exceptions Thrown by a Method, the method must provide a throws clause that lists the exception.

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Code for Exception in Java

    Here is an example of the exception :

    while (noError == false)
    {
    try{
    System.out.println (" Please enter your name : " );
    int x = keyboard.nextInt();
    if ( (string !==null))
    {
    throw new java.lang.Exception();
    }
    else
    {
    noError = true;
    }
    }

    catch (InputMismatchException exception)
    {
    System.err.println (" Wrong input ! Please input characters only !");
    }
    catch (java.lang.Exception ee)
    {
    System.err.println ( " Please enter a valid name, check your name ! " );
    }
    }

    This program will ask the user to enter a name and throws an exception if the user enters the numeric to name field.
    Last edited by Praetor; 15-01-2010 at 09:01 PM.

  5. #5
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Code for Exception in Java

    I am giving an example of throw statement in context. The method removes the top element from the stack and returns the object. The following pop method is taken from a class that implements a common stack object :

    public Object pop() {
    Object obj;

    if (size == 0) {
    throw new EmptyStackException();
    }
    obj = objectAt(size - 1);
    setObjectAt(size - 1, null);
    size--;
    return obj;
    }

Similar Threads

  1. Exception in Java Web Start
    By Jaganmohini in forum Software Development
    Replies: 6
    Last Post: 27-07-2010, 01:15 PM
  2. Malformed exception in java
    By Amy Adams in forum Software Development
    Replies: 5
    Last Post: 24-03-2010, 08:25 AM
  3. Headless Exception in Java
    By Adrina_g in forum Software Development
    Replies: 5
    Last Post: 20-02-2010, 08:36 PM
  4. How to catch an Exception in Java?
    By SKREECH in forum Software Development
    Replies: 4
    Last Post: 18-02-2010, 04:50 AM
  5. Exception in java input/output
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 12:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,045,438.85532 seconds with 17 queries