catching exceptions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cazconv2007
    New Member
    • Feb 2007
    • 30

    #1

    catching exceptions

    hi i need to catch and try exceptions , one for int and one for an array out of bounds i dont undertand how my code is'nt working for array out of bounds heres my code


    import java.util.* ;

    public class ExceptionHomewo rk
    {
    Scanner scan ;
    int data;
    int slot;
    int[] value;

    /**
    * Constructor for objects of class
    * ExceptionHomewo rk
    */
    public ExceptionHomewo rk()
    {
    scan = new Scanner( System.in );
    data=0;
    slot=0 ;
    value = new int[10];
    }



    /**
    * This is the method you must change by adding
    * try-catch blocks
    *
    */
    public void sampleMethod() throws ArrayIndexOutOf BoundsException
    {
    System.out.prin t("Enter the data: ");
    data = scan.nextInt();
    System.out.prin t("Enter the array index: ");
    slot = scan.nextInt();
    value[slot] = data;
    try{
    for(slot =0;slot <10;slot++){
    value[slot] = slot;
    }
    System.out.prin tln(value);
    }
    catch(ArrayInde xOutOfBoundsExc eption e){
    //printed just to inform that we have entered the catch block
    System.out.prin tln("Oops, we went to far, better go back to 0!");
    }
    finally{
    System.out.prin tln(value);
    //method call to continue program
    }

    }
    }

    heres my driver

    public class ExceptionHomewo rkDriver
    {
    public static void main ( String [] args)
    {
    ExceptionHomewo rk exHmwk = new ExceptionHomewo rk();
    exHmwk.sampleMe thod();
    System.out.prin tln("Good By");
    }
    }
  • sandyw
    New Member
    • Mar 2007
    • 122

    #2
    Originally posted by cazconv2007
    hi i need to catch and try exceptions , one for int and one for an array out of bounds i dont undertand how my code is'nt working for array out of bounds heres my code


    import java.util.* ;

    public class ExceptionHomewo rk
    {
    Scanner scan ;
    int data;
    int slot;
    int[] value;

    /**
    * Constructor for objects of class
    * ExceptionHomewo rk
    */
    public ExceptionHomewo rk()
    {
    scan = new Scanner( System.in );
    data=0;
    slot=0 ;
    value = new int[10];
    }



    /**
    * This is the method you must change by adding
    * try-catch blocks
    *
    */
    public void sampleMethod() throws ArrayIndexOutOf BoundsException
    {
    System.out.prin t("Enter the data: ");
    data = scan.nextInt();
    System.out.prin t("Enter the array index: ");
    slot = scan.nextInt();
    value[slot] = data;
    try{
    for(slot =0;slot <10;slot++){
    value[slot] = slot;
    }
    System.out.prin tln(value);
    }
    catch(ArrayInde xOutOfBoundsExc eption e){
    //printed just to inform that we have entered the catch block
    System.out.prin tln("Oops, we went to far, better go back to 0!");
    }
    finally{
    System.out.prin tln(value);
    //method call to continue program
    }

    }
    }

    heres my driver

    public class ExceptionHomewo rkDriver
    {
    public static void main ( String [] args)
    {
    ExceptionHomewo rk exHmwk = new ExceptionHomewo rk();
    exHmwk.sampleMe thod();
    System.out.prin tln("Good By");
    }
    }
    I'm learn this myself...
    Question what does the
    1.
    Code:
    catch(ArrayIndexOutOfBoundsException e){
    e stand for I think you want exception instead of e.
    2. not sure you might need a return
    finally{
    System.out.prin tln(value);
    //method call to continue program
    }
    return //what int you are looking for
    }

    sandyw

    Comment

    • cazconv2007
      New Member
      • Feb 2007
      • 30

      #3
      changed some of the code but now it doesnt like my driver which i dont know why, error says unreported exception java.lang.excep tion must be caught


      import java.util.* ;

      public class ExceptionHomewo rk
      {
      Scanner scan ;
      int data;
      int slot;
      int[] value;

      /**
      * Constructor for objects of class
      * ExceptionHomewo rk
      */
      public ExceptionHomewo rk()
      {
      scan = new Scanner( System.in );
      data=0;
      slot=0 ;
      value = new int[10];
      }



      /**
      * This is the method you must change by adding
      * try-catch blocks
      *
      */
      public void sampleMethod() throws Exception

      {
      try{
      System.out.prin t("Enter the data: ");
      data = scan.nextInt();
      System.out.prin t("Enter the array index: ");
      slot = scan.nextInt();
      value[slot] = data;

      }
      catch (ArrayIndexOutO fBoundsExceptio n e) {
      System.out.prin tln("Error: Array Index Out of Bounds");

      }

      catch (InputMismatchE xception e){
      System.out.prin tln ("Execption enter a number:" + e );
      }

      finally {
      System.out.prin tln(value);


      }
      }


      }

      my driver

      public class ExceptionHomewo rkDriver
      {
      public static void main ( String [] args)
      {
      ExceptionHomewo rk exHmwk = new ExceptionHomewo rk();
      exHmwk.sampleMe thod();
      System.out.prin tln("Good By");
      }
      }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by cazconv2007
        changed some of the code but now it doesnt like my driver which i dont know why, error says unreported exception java.lang.excep tion must be caught


        import java.util.* ;

        public class ExceptionHomewo rk
        {
        Scanner scan ;
        int data;
        int slot;
        int[] value;

        /**
        * Constructor for objects of class
        * ExceptionHomewo rk
        */
        public ExceptionHomewo rk()
        {
        scan = new Scanner( System.in );
        data=0;
        slot=0 ;
        value = new int[10];
        }



        /**
        * This is the method you must change by adding
        * try-catch blocks
        *
        */
        public void sampleMethod() throws Exception

        {
        try{
        System.out.prin t("Enter the data: ");
        data = scan.nextInt();
        System.out.prin t("Enter the array index: ");
        slot = scan.nextInt();
        value[slot] = data;

        }
        catch (ArrayIndexOutO fBoundsExceptio n e) {
        System.out.prin tln("Error: Array Index Out of Bounds");

        }

        catch (InputMismatchE xception e){
        System.out.prin tln ("Execption enter a number:" + e );
        }

        finally {
        System.out.prin tln(value);


        }
        }


        }

        my driver

        public class ExceptionHomewo rkDriver
        {
        public static void main ( String [] args)
        {
        ExceptionHomewo rk exHmwk = new ExceptionHomewo rk();
        exHmwk.sampleMe thod();
        System.out.prin tln("Good By");
        }
        }
        1.)Please use code tags when posting code.
        2.) Read the error message. It is telling what you need to do. You need to add a catch clause that catches Exception.

        Comment

        • cazconv2007
          New Member
          • Feb 2007
          • 30

          #5
          sorry i dont know what code tags are

          i added


          catch (Throwable e) {
          System.out.prin tln ("exception" );
          }

          and tried


          catch (Exception e) {
          System.out.prin tln ("exception" );
          }

          is this what you meant because it didnt work thank for replying tho

          Comment

          • cazconv2007
            New Member
            • Feb 2007
            • 30

            #6
            thanks everyone for your help but finally done it

            Comment

            Working...