Exception for incomparable types?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BurnTard
    New Member
    • May 2007
    • 52

    Exception for incomparable types?

    Okay, new programming language for Burnie. Can anyone tell me how to write a try catch statement for incomparable types? I can't figure out how to get emacs to give me the actual error, and not the very useful description... Basically I want this:

    [CODE=java]
    public class ThisProgram
    {
    public static void main (String[] args)
    {
    String[] list1={"a","b"} ;
    String a="a";
    String b="b";
    String[] everything={a,l ist1,b};

    try
    {
    for (int x=0; x<3; x++)
    {
    System.out.prin tln (everything[x]);
    }
    }
    catch (Incomparable types)
    {
    for (int x=0; x<2; x++)
    {
    System.out.prin tln (list1[x]);
    }
    }
    finally
    {
    continue;
    }
    }
    }
    [/CODE]
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    OK, so your code is a little funny so I had a small chuckle.
    What problem are you trying to solve with it?
    Just to mention a few odd lines:
    String[] everything declares an array of Strings only. You cannot put anything in there that is not a String. In your code you try to put a list1 object which is a String[]. A String[] is not a String so that won't work.

    Then you catch an Incomparable object. You can only catch Objects created from subclasses of Throwable. e.g Exception and it's subclasses. Is your Incomparable declared somewhere and is it a Throwable?
    Also when looping over arrays, use the array length to determine where to stop i.e for (int x=0; x<2; x++) becomes for (int x=0; x<list1.length ; x++)

    What's the finally with the continue inside it supposed to be doing?

    Comment

    • BurnTard
      New Member
      • May 2007
      • 52

      #3
      Yeah, new language for me, so there's going to be quirks to work on...

      The finally isn't supposed to do anything, but it gave me an error if it wasn't there, so... And maybe it was in Ruby or Pyhon or something where you had to fill it with something, so continue was necessary...

      This particular code isn't supposed to solve anything, it's just for learning how stuff works... As for the incomparable thing, that was what this whole post was actually about. I know that isn't correct, and I was asking what the correct exception handler for that is. Trying to put an array into a string array gives the error incomparable types, but that's not the actual exception, is it?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        There is a great post at the top of these forums with a title "Read this First".
        Despite the fact that the article was written by Jos, it actually has a lot of very useful information. Read it and bookmark it.

        Comment

        • N002199B
          New Member
          • Feb 2007
          • 41

          #5
          I guess life can get really difficult. declaring a string variable a="a" and b="a". wow, It has been a while since I saw that one.

          Also, I thought the 'continue' statement is to be used in a loop to disregard any code that follows is but is still in the loop, and the loop action is proceeded to. BurnTard, What is that continue doing in the finally block of code?

          Please read on looping, conditional statements and exceptions in Java.

          Comment

          Working...