Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception compiler error  RSS feed

 
Sami Devine
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
During compilation I get "incompatible types" error for the statement catch(Exception e).

 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you handling Exceptions? I can see nowhere which would throw an Exception in the first place.
 
Yunnan Zhou
Ranch Hand
Posts: 31
C++ Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you have catched Exception,you shouldn't throws Exception.
maybe str.length=0.but there is no exception occur unless you define you own Excetion
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sami Devine wrote:During compilation I get "incompatible types" error for the statement catch(Exception e). . . .
No, you don't. You get a completely different compiler error. Please tell the details correctly.
 
Sami Devine
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,Yunnan thanks
This is an excercise from Exceptions chapter
Campbell, I compiled once again using cmd prompt. The following are the errors

C:\Javasrc>javac Propagate.java
Propagate.java:2: incompatible types
found : Exception
required: java.lang.Throwable
public static void reverse(String reverseStr) throws Exception{
^
Propagate.java:9: incompatible types
found : Exception
required: java.lang.Throwable
catch(Exception e){System.out.println("String length is 0");}
^
2 errors
 
Yunnan Zhou
Ranch Hand
Posts: 31
C++ Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you throw Exception when you start you method.
just remove throws Exception an try again.
or you must try {p.reverse("oops");
p.reverse("");} catch(Exception e){}
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<sorry -- late to the post>

rc
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never got those errors, when I copied and pasted your code, and ran it with the Sun/Oracle compiler in JDK1.6.0_25. I can see nowhere that produces or requires a Throwable. If you comment out the throws declaration, as yunnan zz has pointed out, the whole thing compiles.
Is that the same code you have at home? Which compiler are you using? Which book or tutorial is that exercise from?
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you got your own Exception class, which is different from the java.lang.Exception class? Try changing all instances of Exception to java.lang.Exception?

That is a sure-fire way to cause no end of confusion: create your own class with the same name as a class in the java.lang package.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sami Devine wrote:C:\Javasrc>javac Propagate.java
Propagate.java:2: incompatible types
found : Exception
required: java.lang.Throwable
public static void reverse(String reverseStr) throws Exception{

Exception is a sub-class of Throwable, so that's a bit strange. Do you have another Exception class that you've defined yourself that is masking the built-in java.lang.Exception class?

Edit: 13 seconds! Too slow.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get rid of your own Exception class and it will work. You still need to get rid of that throws, however.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote: . . . 13 seconds! Too slow.
Rob would have been there 3 seconds earlier
 
Sami Devine
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all, there was an Exception.class in my folder which I deleted. I also removed the throws Exception clause. Program compiles and runs.
But it does not throw Exception for the empty string call,
 
Sami Devine
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding an else part solved the problem

 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of observations, if you're interested in improving the code:

You throw an Exception, but then just catch it and write some output. Why not just write the output in the first place?

If you do need to use an exception (which would be most likely if you're throwing the exception out of the method), you're better off using a more specific exception so that it is more meaningful, and so that people can catch particular exceptions if they want to. In this case, I'd suggest using an IllegalArgumentException. That's an unchecked exception, so it doesn't have to be caught.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the point of that exercise is to show how Exceptions are thrown and caught, but there is no need to worry about the empty String. If you go through the for loop with an empty String, and write the indices and the control flow with a pencil and paper, you will see exactly what happens and you will see there will never be any problems like that
 
Sami Devine
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell, I think if the string length is 0, the for loop will not be executed at all.

Mathew, I understand, this is one of my first programs with exceptions. So it is a desperate attempt to use try catch finally. I looked at the program once again, it does look weird. probably the weirdest solution for this problem.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sami Devine wrote:Campbell, I think if the string length is 0, the for loop will not be executed at all. . . .
Correct.

If the loop is never executed, the loop cannot suffer any problems and if it can't suffer any problems it can't throw any Exceptions.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!