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 6 of 6

Thread: Exception problem...plzz help

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Exception problem...plzz help

    Here is my code snippet ----->
    class A
    {
        void disp() throws ArithmeticException
        {
            throw new ArithmeticException();
        }
    }
     
    class B extends A
    {
        public void disp()
        {
            System.out.println("Hello");
        }
    }
    public class Test 
    {
     
        public static void main(String[] args) {
           A a = new B();
           a.disp();
     
       }


    Here, @ a.disp(), compilation err : unreported exception, java.lang.Exception, must be caught or declared to be thrown.
    Here, I am calling method of Class A which does not throw any exception.
    So, why this err??
    Plzz plzz help.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Exception problem...plzz help

    Quote Originally Posted by Arati2512 View Post
    Here, @ a.disp(), compilation err : unreported exception, java.lang.Exception, must be caught or declared to be thrown.
    Here, I am calling method of Class A which does not throw any exception.
    So, why this err??
    Plzz plzz help.
    Hello Arati2512!
    I tried your code and it runs without any error. Are you sure this is the code that is causing the error?

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Exception problem...plzz help

    Oops....My mistake! It should be like this ---->

    class A
    void disp() throws Exception
    {
    throw new Exception();
    }

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Exception problem...plzz help

    As the error message says java.lang.Exception, must be caught or declared to be thrown.
    You should either declare it by adding a throws clause in your main like you do in your disp() method or catch it by placing the statement that may cause the exception (here the method call) inside a try...catch block.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Exception problem...plzz help

    ya...but why?
    Because it is not necessary. If the base class is throwing exception, then derived class may or may not throw. And I am calling derived class's method on object.

  6. #6
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Exception problem...plzz help

    Quote Originally Posted by Arati2512 View Post
    ya...but why?
    Because it is not necessary. If the base class is throwing exception, then derived class may or may not throw. And I am calling derived class's method on object.
    I'm not sure what is your exact question. As you said derived class may or may not throw. Therefore because of the *may throw*, main should declare or catch the exception. If you are asking why an exception, must be caught or declared to be thrown I'm not the appropriate to tell you why is this a java requirement, but here is an interesting link.

Similar Threads

  1. can someone help me debug plzz
    By andys in forum Object Oriented Programming
    Replies: 1
    Last Post: November 27th, 2010, 04:37 PM
  2. can' stop working a part of code.plzz help
    By kyros in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 03:10 PM
  3. plz help me with this.....plzz....
    By renny in forum Java Theory & Questions
    Replies: 25
    Last Post: May 18th, 2010, 08:19 AM
  4. Exception-Based Problem
    By Hax007 in forum Exceptions
    Replies: 1
    Last Post: December 10th, 2009, 02:53 AM
  5. ymsg exception problem
    By maruf10 in forum Java Networking
    Replies: 0
    Last Post: September 19th, 2009, 01:05 PM