Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By JosAH

Thread: How to create exception object in java?

  1. #1
    Java Example is offline Moderator
    Join Date
    Sep 2014
    Posts
    27
    Rep Power
    0

    Default How to create exception object in java?

    In this tutorial , we will discuss how to create exception object.When we try to access a value of an object which is out of range , we get ExceptionObject error.In the example below,we have declare an integer array of size 2.When we try to access the 7th index of the array which the array does not hold,exception occurs in the program.Here ,we have displayed the exception by creating the object of class exception.The ex.printStackTrace() method of the class Throwable is used which Prints this throwable error to the standard error stream.

    Java Code: To create Exception object
    public class Exceptionobject {
    
        public static void main(String[] args) {
            try {
                int arr[];
                arr = new int[2];
                arr[0] = 23;
                arr[1] = 23;
                System.out.println("Element of array at this index is " + arr[7]);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
    Output of the program
    java.lang.ArrayIndexOutOfBoundsException: 7
    at Exceptionobject.main(Exceptionobject.java:10)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: How to create exception object in java?

    You don't get an ExceptionObject error; you get an ArrayOutOfBoundsException; and puhlease, keep your (incorrect) trivialities to yourself; for some reason the owner of this forum made you a Super Moderator but your silly examples that are posted here by the truck load don't help at all. It would be very much appreciated if you'd stop it.

    Jos
    DarrylBurke likes this.
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. trying to create an object
    By mbw290 in forum New To Java
    Replies: 5
    Last Post: 12-07-2012, 03:02 PM
  2. What is best object to create?
    By lam5442 in forum New To Java
    Replies: 1
    Last Post: 02-23-2011, 09:44 PM
  3. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  4. create object
    By paul21 in forum New To Java
    Replies: 4
    Last Post: 03-07-2010, 07:14 PM
  5. How to create your own Exception class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:40 PM

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
  •