Results 1 to 15 of 15
Thread: Exception
- 01-07-2011, 10:17 AM #1
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
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?
- 01-07-2011, 11:06 AM #2
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,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-07-2011, 11:31 AM #3
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
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
- 01-07-2011, 11:52 AM #4
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)
- 01-07-2011, 11:54 AM #5
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.
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-07-2011, 12:16 PM #6
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
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"????
- 01-07-2011, 12:31 PM #7
When I ran your code, while throwing exception, it gave me,
While throwing Throwable, 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
I really don't know from which code you are getting your version of exception on your console.Java Code:ExceptionTest.java:14: unreported exception java.lang.Throwable; must be caught or declared to be thrown throw new Throwable(); //line2 ^ 1 error
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-07-2011, 01:21 PM #8
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
thanks goldest
now i added import java.lang.Exception
and works fine
- 01-07-2011, 01:24 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
I get unhandled exception as well (as expected), so I guess it's a case of how are you compiling this?
- 01-07-2011, 01:24 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
You don't need to import anything from lang.
That's not the solution.
- 01-07-2011, 01:32 PM #11
Java Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-10-2011, 06:23 AM #12
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
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.....
- 01-10-2011, 06:39 AM #13
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
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....
- 01-10-2011, 07:56 AM #14
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.
In case, if you want to use same name classes, you need to use their full names in your code. For e.g.
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.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); } }
Hope that helps,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-10-2011, 08:59 AM #15
Member
- Join Date
- May 2010
- Posts
- 90
- Rep Power
- 0
Similar Threads
-
Exception in thread "main" java.lang Exception In InitializerError
By kenzo2009 in forum New To JavaReplies: 4Last Post: 10-25-2010, 07:42 PM -
exception
By thamizhisai in forum Advanced JavaReplies: 9Last Post: 05-30-2008, 08:47 AM -
why this exception
By payal.mitra86 in forum JDBCReplies: 1Last Post: 05-21-2008, 10:28 PM -
Exception
By rmaadil in forum JDBCReplies: 1Last Post: 05-19-2008, 12:45 PM -
Trouble with factory method - unhandled exception type Exception
By desmond5 in forum New To JavaReplies: 1Last Post: 03-08-2008, 06:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks