1 2 3 Previous Next 34 Replies Latest reply: Jul 1, 2011 6:05 PM by jschellSomeoneStoleMyAlias Go to original post RSS
      • 15. Re: Why does Exception allows Throwable as a cause
        864321
        I think we are going a bit off topic here. I agree there might be scenarios where you would want to handle Error. What confuses me is why allow an Error to be a cause of Exception? This is a basic design question.

        Error is not meant to be handled within the SAME Thread if that makes more sence.
        • 16. Re: Why does Exception allows Throwable as a cause
          796440
          yogesh wrote:
          I think we are going a bit off topic here. I agree there might be scenarios where you would want to handle Error. What confuses me is why allow an Error to be a cause of Exception? This is a basic design question.
          Why not? If you can catch something, you may want to wrap it and rethrow it as a different Throwable. The "cause" field is there for exactly that purpose--so you can throw a more appropriate exception, but still have the information available as to what the original problem was.

          Why would you exclude Error from the possible "cause" Throwables? Sure, as a design issue, it's probably uncommon to maybe even never that you'd want to do that, but that doesn't warrant complicating the language or API by excluding that particular corner of the tree.
          Error is not meant to be handled within the SAME Thread
          Says who? And why would that be relevant?

          Honestly, I think you're making a mountain out of a molehill here. The basic idea is, "We want to be able to construct an exception that indicates some other exception as the underlying cause." Simple. Boom. Done. There would be no benefit to adding, "...but I can't at this particular time imagine why somebody would want to use Error as a cause, so I'll take the extra step of disallowing it." That just makes no sense, but you are effectively asking, "Why didn't they do that?"
          • 17. Re: Why does Exception allows Throwable as a cause
            864321
            >
            "We want to be able to construct an exception that indicates some other exception as the underlying cause."
            >

            Thats exactly my point. Error's are not exceptions. The design gives a suggestion that it is OK to wrap Throwables that are not exceptions. There is a conceptual difference between Exceptions and Errors (otherwise we wouldn't need a different branch in the throwable hierarchy just for Errors.

            >
            "Honestly, I think you're making a mountain out of a molehill here."
            >

            Well I still can't think of a business requirement for this.

            Well, if you are satisfied with your answer, so be it.

            Cheers.

            Edited by: yogesh on Jul 1, 2011 1:58 AM
            • 18. Re: Why does Exception allows Throwable as a cause
              EJP
              The design gives a suggestion that it is OK to wrap Throwables that are not exceptions.
              More accurately, the design allows for the possibilty that Exceptions are not the only Throwables you might want to wrap. Unless you have a cast-iron proof that this isn't the case, the entire discussion is completely pointless.
              There is a conceptual difference between Exceptions and Errors (otherwise we wouldn't need a different branch in the throwable hierarchy just for Errors.
              Correct.

              So?
              • 19. Re: Why does Exception allows Throwable as a cause
                864321
                A business scenario where this could be useful perhaps?
                Please read and understand the question before you decide that you know the answer and should comment.
                • 20. Re: Why does Exception allows Throwable as a cause
                  gimbal2
                  yogesh wrote:
                  What confuses me is why allow an Error to be a cause of Exception? This is a basic design question.
                  How about KISS? Exception handling, even though it can lead to hairy code sometimes, to me is a wonderfully simple concept. Lets keep it that way. No more additional rules, no complex designs, no nothing. Keep it simple, silly!

                  The only thing I wonder about is: why Error? The word 'error' to me is less destructive than 'exception', yet in Java terms an Error is actually worse than an Exception. I would have named it "disaster". OutOfMemoryDisaster. Yeah. Also works nice when Java goes into outer space. SolarFlareDisaster. MeteorShowerDisaster. Nice!

                  Sorry, a bit of a giddy response I admit. But what the heck, its Friday. Thats no day for arguments, its a day for counting down hours while trying to do as little work as possible.
                  • 21. Re: Why does Exception allows Throwable as a cause
                    864321
                    Cheers Gimbal ;)
                    • 22. Re: Why does Exception allows Throwable as a cause
                      gimbal2
                      yogesh wrote:
                      Cheers Gimbal ;)
                      Group hug!
                      • 23. Re: Why does Exception allows Throwable as a cause
                        EJP
                        yogesh wrote:
                        A business scenario where this could be useful perhaps?
                        Please read and understand the question before you decide that you know the answer and should comment.
                        I didn't write any of that. Please read what I write, and quote it accurately, when you reply to my posts. I asked you for a cast-iron proof that Exceptions are the only things you ever want to wrap in other Exceptions. You haven't provided it. Persiflage won't do.
                        • 24. Re: Why does Exception allows Throwable as a cause
                          jtahlborn
                          yogesh wrote:
                          I think we are going a bit off topic here. I agree there might be scenarios where you would want to handle Error. What confuses me is why allow an Error to be a cause of Exception? This is a basic design question.

                          Error is not meant to be handled within the SAME Thread if that makes more sence.
                          actually, i answered your question in the first reply. some of the followup posts went on to a side topic, but your original question has already been answered with an example direct from the jdk.
                          • 25. Re: Why does Exception allows Throwable as a cause
                            796440
                            yogesh wrote:
                            >
                            "We want to be able to construct an exception that indicates some other exception as the underlying cause."
                            >

                            Thats exactly my point. Error's are not exceptions.
                            Yes, they are. Notice I said "exception" with a lowercase "e"--the general meaning, not specifically the Exception class.
                            The design gives a suggestion that it is OK to wrap Throwables that are not exceptions.
                            Because it is. Why do you think it wouldn't be?
                            There is a conceptual difference between Exceptions and Errors (otherwise we wouldn't need a different branch in the throwable hierarchy just for Errors.
                            There's also a similarity, else they wouldn't all be Throwables.

                            >
                            >
                            "Honestly, I think you're making a mountain out of a molehill here."
                            >

                            Well I still can't think of a business requirement for this.
                            You're looking at it backwards. Your view seems to be, "I can't personally think of a use for this, so the language should take the extra step to disallow it." The simplest, most straightforward approach is for the base class to allow any Throwable as a cause. The fact that some Throwables may be unlikely to make sensible causes is not a legitimate reason to take the special step of disallowing them.
                            Well, if you are satisfied with your answer, so be it.
                            I am, and I have been for the dozen or more years I've been working with Java and its exceptions. I wasn't really looking for your approval, but thanks anyway.
                            • 26. Re: Why does Exception allows Throwable as a cause
                              864321
                              because sometimes you do want to catch everything. one example is when you want to transfer failures from one thread context to another, e.g. FutureTask. FutureTask catches Throwable within its run() method. that Throwable is then transferred to the context of another thread by being thrown, wrapped as an ExecutionException, to the caller of the FutureTask.get() method.
                              well, catching would be a different context. Yes, sometimes I would want to catch a Throwable. What I wouldn't want to do is, throw an Exception when the actual cause was an Error.
                              • 27. Re: Why does Exception allows Throwable as a cause
                                796440
                                yogesh wrote:
                                because sometimes you do want to catch everything. one example is when you want to transfer failures from one thread context to another, e.g. FutureTask. FutureTask catches Throwable within its run() method. that Throwable is then transferred to the context of another thread by being thrown, wrapped as an ExecutionException, to the caller of the FutureTask.get() method.
                                well, catching would be a different context. Yes, sometimes I would want to catch a Throwable. What I wouldn't want to do is, throw an Exception when the actual cause was an Error.
                                So you, at this moment, with your vast experience in Java development, cannot picture a scenario where you'd do throw Something("Oh no!", someError) and therefore, you think it would be better for the API not to allow anybody to ever do that?
                                • 28. Re: Why does Exception allows Throwable as a cause
                                  864321
                                  I think the discussion has gone way ahead. Thanks everyone for their valuable comments.

                                  Although I never meant to suggest "Because I feel It shouldn't be allowed, they should remove it".

                                  My question was basically to find out the actual reason for allowing it.

                                  I guess, no one has ever come across a situation when they would wrap an Error in an Exception. And there is no good reason (please dont honk your horns if I may say so) behind allowing a Throwable as a cause.

                                  Having said that. Thanks everyone.

                                  regards and best wishes
                                  Yogesh
                                  • 29. Re: Why does Exception allows Throwable as a cause
                                    796440
                                    yogesh wrote:
                                    My question was basically to find out the actual reason for allowing it.
                                    That's already been answered: Because it's simpler to allow it than to disallow it, and there's no good reason to disallow it.
                                    I guess, no one has ever come across a situation when they would wrap an Error in an Exception.
                                    Nobody said that. I may have done that. I can't think of a specific case where I would, but I can't say that I wouldn't either. Also, I believe jtahlborn provided an example in the core API where they do just that.
                                    And there is no good reason,
                                    False. You can't think of a good reason at the moment. That doesn't mean there aren't valid use cases for doing so.
                                    Please dont honk your horns if I may say so, to allow a Throwable as a cause.
                                    Eh? I consider it a good thing that it is allowed, because there can be situations where it is called for, and there's no good reason for the API to disallow it.

                                    Edited by: jverd on Jul 1, 2011 12:59 PM