Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: error in my exception handling program. unable to understand this program , please help. Thanx in advance

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default error in my exception handling program. unable to understand this program , please help. Thanx in advance

    package javaapplication6;
    import java.util.Scanner; 
    public class JavaApplication6 {
     
        public static void main(String[] args) {
     
              int a,b,c;
              int arr[] = new int[4];
              Scanner ob  = new Scanner(System.in);
              System.out.println("Enter 2 nums");
              a = ob.nextInt();
              b= ob.nextInt();
    try
    {
        arr[b] = 100;
        c=a/b;
        System.out.println(c);
    }catch(Exception e )
    {
      //  System.out.println("Error "+e.getMessage());
        if(e.instanceof ArithmeticException)
        {
            System.out.println("Divide by zero");
        }
        if(e.instanceof ArrayIndexOutOfBounds)
        {
     
            System.out.println("Index not valid");
     
        }
     }
    }
    }
    Last edited by CruiseDevice; August 17th, 2014 at 05:11 AM.


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: error in my exception handling program. unable to understand this program , please help. Thanx in advance

    Are you getting any error messages? What do you think
    the issue is? Btw, please use code tags when posting code
    Thank you.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: error in my exception handling program. unable to understand this program , please help. Thanx in advance

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

  4. #4
    Junior Member
    Join Date
    Aug 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: error in my exception handling program. unable to understand this program , please help. Thanx in advance


  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: error in my exception handling program. unable to understand this program , please help. Thanx in advance

    Your screen capture is unreadable. Mouse into it, select the needed text, and copy and paste it directly into a post.

  6. #6
    Junior Member
    Join Date
    Aug 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: error in my exception handling program. unable to understand this program , please help. Thanx in advance

    if(e.instanceof ArithmeticException)
    {
    System.out.println("Divide by zero");
    }
    if(e.instanceof ArrayIndexOutOfBounds)
    {


    It is showing error in there two loops.

    Well the program is working. But it is not throwing exception.

  7. #7
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: error in my exception handling program. unable to understand this program , please help. Thanx in advance

    Debugged your code and got this:

    <Identifier>Expected after expression on this line of code:

    if(e.instanceof ArithmeticException)
    Basically, the compiler was looking for something on that line
    (or near it) that should of been there. Generally referred to as a
    'symbol'. Check the code and make sure your are using the correct
    symbol names when calling instances and make sure all the ones that
    expected to be visible at compile-time are visible.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  8. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    CruiseDevice (August 17th, 2014)

  9. #8
    Junior Member
    Join Date
    Aug 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: error in my exception handling program. unable to understand this program , please help. Thanx in advance

    Is there any connection between packages and exception handling in java. Means is it necessary to create a package before trying exception handling examples?

  10. #9
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: error in my exception handling program. unable to understand this program , please help. Thanx in advance

    Quote Originally Posted by CruiseDevice View Post
    Is there any connection between packages and exception handling in java. Means is it necessary to create a package before trying exception handling examples?
    No. A package is used to 'pack' a group of project files together that then form
    the application. Every source file related to that application must have the same
    package name as all the other source files. Packages are very useful in having
    larger applications that contain various classes. It's a bit like C++ where classes
    have "header files" and the header file had to have the same name as the source file
    in order for the compiler to find it.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

Similar Threads

  1. Replies: 3
    Last Post: September 17th, 2021, 12:10 AM
  2. unable to understand that how control transfer in this program .
    By gautammuktsar@gmail.com in forum Threads
    Replies: 1
    Last Post: March 12th, 2014, 05:16 PM
  3. Help me understand the program! pls
    By YogiB1810 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 14th, 2013, 07:50 AM
  4. unable to make thi work - program connectfour
    By sumoon in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 14th, 2013, 04:23 AM
  5. Unable to execute the program (no compilation error)
    By Alexie91 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 16th, 2011, 02:39 PM

Tags for this Thread