Results 1 to 15 of 15

Thread: Exception

  1. #1
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default Exception

    hi friends

    i am confused with strange behaviour of this program.


    import java.lang.*;

    public class ExceptionTest
    {
    public static void main(String ... args)
    {
    ExceptionTest obj = new ExceptionTest();
    obj.method1();
    }

    void method1()
    {
    throw new Exception(); //line1
    //throw new Throwable(); //line2
    }

    }



    as we know Throwable is the superclass of Exception. in the above code it gives error on compiling as

    ExceptionTest.java:21: incompatible types
    found : Exception
    required: java.lang.Throwable
    throw new Exception();
    ^
    1 error

    but if we uncomment line 2, and comment line 1 then error will be

    ExceptionTest.java:22: unreported exception java.lang.Throwable; must be caught
    or declared to be thrown
    throw new Throwable();
    ^
    1 error

    why does it provides a eoor saying incompatible types on throwing new exception()?
    i expected following error in first case

    ExceptionTest.java:21: unreported exception java.lang.Exception; must be caught
    or declared to be thrown
    throw new Exception();
    ^
    1 error

    whats happening here?

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    The thumb rule is that, when you are throwing the exception, you should declare the same in your method signature.

    Does that ring the bell for you?

    If it still doesn't, refer some good documentation here: How To Throw Exception

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    yes thats right and i expect that itself...
    but the error is not that

    error produced is

    ExceptionTest.java:21: incompatible types
    found : Exception
    required: java.lang.Throwable
    throw new Exception();
    ^
    1 error

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    first of all your import java.lang.* is for nothing, since java.lang is imported by default. secondly if your code throw a exception you must handle it 1) by add throw declaration or by surrounding it with try/catch. the corrections i have made is with the try/catch block and your code will be compilable and runnable and produce

    java.lang.Exception
    at fuel.ExceptionTest.method1(ExceptionTest.java:11)
    at fuel.ExceptionTest.main(ExceptionTest.java:6)

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    The rule is simple!

    If you want to throw Exception, have your method1() to throw it by using throws Exception. And then throw new Exception() inside that.

    If you want to throw Throwable, have your method1() to throw it by using throws Throwable. And then throw new Throwable() inside that.

    Make sure that you either wrap the call to method1() in a try/catch block inside your main method OR have your main method throws the Throwable.

    If you prefer to put your method1() inside try/catch, then make sure to catch (Exception e) when you are throwing Exception and make sure to catch (Throwable t) when you are throwing Throwable.

    I don't see any issue in either approach.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  6. #6
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    i know it is throw an exception which is never caught, or throws and sure we get an error
    but why produce an error like this?

    ExceptionTest.java:21: incompatible types
    found : Exception
    required: java.lang.Throwable
    throw new Exception();

    why not produce an error like " unreported exception java.lang.Exception; must be caught or declared to be thrown"????

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    When I ran your code, while throwing exception, it gave me,
    Java Code:
    ExceptionTest.java:13: unreported exception java.lang.Exception; must be caught
    or declared to be thrown
    throw new Exception(); //line1
    ^
    1 error
    While throwing Throwable, it gave me,
    Java Code:
    ExceptionTest.java:14: unreported exception java.lang.Throwable; must be caught
    or declared to be thrown
    throw new Throwable();   //line2
    ^
    1 error
    I really don't know from which code you are getting your version of exception on your console.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  8. #8
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    thanks goldest

    now i added import java.lang.Exception
    and works fine

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

    Default

    I get unhandled exception as well (as expected), so I guess it's a case of how are you compiling this?

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

    Default

    You don't need to import anything from lang.
    That's not the solution.

  11. #11
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by jomypgeorge View Post
    now i added import java.lang.Exception
    and works fine
    java.lang package comes default for any java class. We don't need to import it explicitly.

    Java Code:
    import java.lang.*;
    The import above just doesn't make any difference.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  12. #12
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    i think you are using some IDE like eclipse.
    try it in a text file and compile using javac command.

    i think you can then see the difference.....

  13. #13
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    at last i got the reason,
    i have already created a class file with name Exception in my default package.
    and compiler used it where i used Exception......
    now i deleted Exception.class and now it works as expected.

    but one doubt now....
    how compiler choose one class if same class name exists in more than one package?


    thanks a lot for you all.... sorry for the mistake....

  14. #14
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    That's why packages are meant to be, to organize your classes in proper manner. So that any naming collision will be avoided.

    You are not expected to have the classes with same name as in java.lang package, because that will make the compiler freak out. What you were trying to do is to have Exception class in your package and the same name class is already present in java.lang package, so the errors were expected to come.


    Quote Originally Posted by jomypgeorge View Post
    but one doubt now....
    how compiler choose one class if same class name exists in more than one package?
    In case, if you want to use same name classes, you need to use their full names in your code. For e.g.

    Java Code:
    import java.util.Date;
    
    public class SameNameClasses{
    	public static void main(String args[]) {
    		Date d = new Date();
    
    		java.sql.Date d1 = new java.sql.Date(4);
    	}
    }
    In above snippet, we are using Date class from both java.util and java.sql packages. If you want to use same name classes in your code, one can be imported the normal way, and rest need to be used with their full names as shown above.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  15. #15
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 10-25-2010, 08:42 PM
  2. exception
    By thamizhisai in forum Advanced Java
    Replies: 9
    Last Post: 05-30-2008, 09:47 AM
  3. why this exception
    By payal.mitra86 in forum JDBC
    Replies: 1
    Last Post: 05-21-2008, 11:28 PM
  4. Exception
    By rmaadil in forum JDBC
    Replies: 1
    Last Post: 05-19-2008, 01:45 PM
  5. Replies: 1
    Last Post: 03-08-2008, 07:41 PM

Posting Permissions

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