In this case, I would rethrow an AssertionError since you assume that the exception can't be raised. Don't forget to use the initCause() method to propagate the information ( AssertionError doesn't have a constructor accepting a Throwable ).
In this case, I would rethrow an AssertionError since you assume that the exception can't be raised. Don't forget to use the initCause() method to propagate the information (AssertionError doesn't have a constructor accepting a Throwable).
1 AssertionError has a constructor accepting an Object, which will be used as the cause if it's Throwable. (Random side note: I believe they're improving this in JDK7. ) – ColinD Sep 30 '10 at 17:03 @ColinD Wow... I've just learned something.Thanks.
– gawi Sep 30 '10 at 17:39.
You can always throw a RuntimeException, or one derived from it, even from a method not explicitly declaring itself to throw exceptions.
This is contract of Comparator. Compare method. If you want to use it you should just follow the rule not to throw checked exceptions from it :) Meanwhile, you can throw unchecked exception (RuntimeException or its subclass) and this will not break contract.
You ask different questions in the question title and in the question body. You weren't clear on why the exception-capable function used by the compare() method would throw an exception. It's either because there are certain uncomparable objects in the collection (like a NaN numberic value) or else it's because there are certain pairs of objects that can't compared against each other.
Why can't I throw an exception in the Comparator? I would guess Comparator.compare() isn't designed to throw a checked exception because: it is assumed that any items that you would wish to compare/sort would always be comparable. If a Comparator.compare() could throw some sort of expected (i.e.
Checked) exception, then I can envision a couple of undesirable scenarios: a. A sort could abort because there is some sort of uncomparable object in there - the likely response being to remove the uncomparable object(s) and try the sort again b. Multiple sorts on the different orderings of the same collection of objects could sometimes abort with an exception and sometimes succeed depending on whether or not a pair uncomparable objects happened to come up for comparison during the sort This is of course just my conjecture.
How would I solve this? I going to assume that the reason the exception-possible function that your Comparator.compare() uses throws an exception is because there's an uncomparable object in the collection (like a NaN numberic value). Options include: Sort a copy of the list with the uncomparable object(s) removed.
Throw an unchecked (runtime) exception to abort the sort. Not sure what you would do then other than #1 above. Follow the NaN approach and make it so those objects come out at beginning or end.
NaN values are normally uncomparable to other values, but during a sort, the comparator defines its own total ordering so that NaN values end up at the end of the sorted collection. download.oracle.com/javase/1.4.2/docs/ap...) ... The CompareTo(java.lang. Double).
... This ordering differs from the For the purposes of sorting, all NaN values are considered equivalent and equal. To do this, code your Comparator.compare() such that any incomparable object always compares greater than any comparable object and it always compares equal to any other incomparable object.
It's because you haven't annotated your method with Lombok's @SneakyThrows annotation. Check it out at projectlombok.org/features/SneakyThrows.html" rel="nofollow">projectlombok.org/features/SneakyThrows.... The demo and slides on the Lombok homepage are also worth a look projectlombok.org.
There are 2 ways of solving this: Catching the exception and throwing it inside a java.lang. RuntimeException(), or Catching the exception and logging it using Log4J or SLF4J (or any logger factory you feel comfortable with). The contract for comparator's compare method doesn't throw exceptions.
2 I would advise against logging the error and continue. This is not the kind of error you should ignore. – gawi Sep 30 '10 at 16:58 I do agree with you, but you're just doing some compare, so in essence, the compare method assume that no exceptions should be thrown.
Plus, by logging the exception, you have a stacktrace in file to show you where the exception occurred. – The Elite Gentleman Sep 30 '10 at 17:04 The logging approach would tell you where the first of your exceptions occured, because it pretty likely that more exceptions will be raised when exceptions are ignored (and yes, logging it and keep working = ignoring). – whiskeysierra Sep 30 '10 at 23:19 Besides, you're comparing 2 objects (which effectively will use the object's equals() method), so chances of exceptions thrown are minimal.
Log the exception & keep working helps in a way that you later realise an exception was thrown. The coder can simply return a -1 if an exception was thrown. Logging gives much info in file, etc.– The Elite Gentleman Sep 30 '10 at 6:52.
You can rethrow a checked exception and avoid compilation error by using a number of tricks. The simplest is; try { // something } catch (Exception e) { Thread.currentThread(). Stop(e); } However, since the compiler has no idea you have done this.
You can confuse it and yourself if you are not careful. One of the objectives of closures is to handle checked exceptions in things like Comparators correctly (while others would prefer they went away).
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.