Results 1 to 7 of 7
  1. #1
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default exception handling

    I am confused on how to implement a exception handling. Am i doing this right?

    Java Code:
    /**The fish's location (row or col) is adjusted by ONE unit, depending on the 
    	 * fish's current direction.  For example, if the current direction is "UP", then
    	 * the fish's row should be decremented.
    	 * <p>
    	 * If the fish's current direction is not equal to one of the static constants UP, 
    	 * DOWN, LEFT, or RIGHT, then this method will throw an 
    	 * IllegalFishDirectionException, passing the fish's direction to the constructor
    	 * of that exception when you create it. 
    	 */
    	public void move() {
    
    		int position = this.getDirection();
    		if (position != UP 
    				|| 
    				position != DOWN
    				||
    				position != LEFT
    				||
    				position != RIGHT){
    
    			throw new IllegalFishDirectionException(position);
    		}
    		else if (position == UP){
    			row--;
    			
    		}
    		else if(position == DOWN){
    			row++;
    		}
    		else if (position == LEFT){
    			col++;
    		}
    		else{
    			col--;
    		}
    	}
    THanks for help

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: exception handling

    you can do it that way yes. Also look up Try Catch blocks, there are also a way to handle exceptions.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: exception handling

    In your case, the exception will always be thrown. Consider position UP, since position != DOWN is true, the if-condition will be true and a new IllegalFishDirectionException is thrown. That is the case for all positions. You should change the || to &&: when the position is not UP and not DOWN and not LEFT and not RIGHT, then and only then, the Exception should be thrown.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: exception handling

    To add: you should also consider validating the value and throwing the Exception at the point in which the 'position' variable is set (eg in a setDirection method). Alternatively, use an enum which will enforce the constraint at compile time rather than runtime.

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: exception handling

    To make life easier for yourself, you could throw the exception when all possible values have been handled. Any other value would be illegal at that point and you can throw the exception. This saves yourself testing all different combinations and options, like this:
    Java Code:
    public void move() {
     
            int position = this.getDirection();
            if (position == UP){
                row--;
            }
            else if(position == DOWN){
                row++;
            }
            else if (position == LEFT){
                col++;
            }
            else if (position == RIGHT) {
                col--;
            }
            else {
                throw new IllegalFishDirectionException(position);
            }
        }

  6. #6
    teja is offline Member
    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Default Re: exception handling

    This is my code to implement stack using arrays!!But i am getting an exception array out of bounds for push() method...help me wid dis!!
    import java.io.*;
    import java.util.*;
    public class Stack {
    int item,top=-1;
    int a[]=new int[item];
    public void push()
    {


    top++;
    System.out.println("enter the item");
    Scanner sc1=new Scanner(System.in);
    item=sc1.nextInt();
    a[top]=item;
    System.out.println("push operation performed");
    }
    public int pop()
    {

    item=a[top];
    top--;
    return item;
    }
    public void display()
    {

    for(int i=top;i>=0;i--)
    {
    System.out.println(i);
    }
    }

    public static void main(String args[])throws IOException
    {


    int ch;
    String che;
    Stack s1=new Stack();
    Stack s2=new Stack();
    Stack s3=new Stack();
    do{
    System.out.println("1.push 2.pop 3.display \n Enter your choice");
    Scanner sc2=new Scanner(System.in);
    ch=sc2.nextInt();
    switch(ch)
    {
    case 1:
    s1.push();
    break;
    case 2:
    s2.pop();
    break;
    case 3:
    s3.display();
    break;
    }
    System.out.println("continue y or n");
    Scanner sc3=new Scanner(System.in);
    che=sc3.next();
    sc3.close();
    }
    while(che=="y"||che=="Y");
    }

    }

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: exception handling

    Please do not post multiple times on the same subject, and please don't hijack someon else's thread.
    I have moved your other post from Java Certification into this sub-forum, so continue int here please (and read what I wrote there as well).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Exception Handling
    By Bicentric in forum New To Java
    Replies: 1
    Last Post: 07-22-2012, 09:37 PM
  2. Exception handling
    By Moedig in forum New To Java
    Replies: 2
    Last Post: 11-10-2011, 10:43 PM
  3. Exception Handling
    By eLancaster in forum New To Java
    Replies: 4
    Last Post: 02-20-2011, 12:00 AM
  4. Exception Handling help
    By MZA in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 09:23 AM
  5. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 03:13 AM

Tags for this Thread

Posting Permissions

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