Why doesn't Java have an EmptyQueueException? [closed]
up vote
24
down vote
favorite
In the pop
method of java.util.Stack
, it throws an EmptyStackException
if the Stack is empty. But the remove
method of java.util.Queue
(which is similar to pop
in the Stack
class) instead throws a NoSuchElementException
. Why is there this inconsistency in Java?
java
closed as primarily opinion-based by Stephen C, Boann, Moira, John Dvorak, maytham-ɯɐɥʇʎɐɯ Nov 30 at 19:27
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
24
down vote
favorite
In the pop
method of java.util.Stack
, it throws an EmptyStackException
if the Stack is empty. But the remove
method of java.util.Queue
(which is similar to pop
in the Stack
class) instead throws a NoSuchElementException
. Why is there this inconsistency in Java?
java
closed as primarily opinion-based by Stephen C, Boann, Moira, John Dvorak, maytham-ɯɐɥʇʎɐɯ Nov 30 at 19:27
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
24
down vote
favorite
up vote
24
down vote
favorite
In the pop
method of java.util.Stack
, it throws an EmptyStackException
if the Stack is empty. But the remove
method of java.util.Queue
(which is similar to pop
in the Stack
class) instead throws a NoSuchElementException
. Why is there this inconsistency in Java?
java
In the pop
method of java.util.Stack
, it throws an EmptyStackException
if the Stack is empty. But the remove
method of java.util.Queue
(which is similar to pop
in the Stack
class) instead throws a NoSuchElementException
. Why is there this inconsistency in Java?
java
java
edited Nov 30 at 13:39
Boann
36.6k1287121
36.6k1287121
asked Nov 30 at 4:01
Hai Hoang
551415
551415
closed as primarily opinion-based by Stephen C, Boann, Moira, John Dvorak, maytham-ɯɐɥʇʎɐɯ Nov 30 at 19:27
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as primarily opinion-based by Stephen C, Boann, Moira, John Dvorak, maytham-ɯɐɥʇʎɐɯ Nov 30 at 19:27
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
34
down vote
accepted
The Stack
class is a legacy class from the Java 1.0 days, prior to the introduction of the collections framework. Its interface has to be backwards compatible ... and that is how it was designed.
By contrast, the Queue
interface was introduced in the Java 1.5 revision of the collections framework. By that time, the NoSuchElementException
had been chosen by the designers as the best way to express this kind of error condition1.
Note that NoSuchElementException
could have been used in Stack
since both classes existed in Java 1.0, but clearly, the designers had other ideas back then2.
So this is just a historical anomaly that has arisen due to the way that the Java APIs have evolved. It cannot be fixed without breaking binary compatibility for existing applications that use the Stack
class.
1 - You may disagree with that, but you asked why, and this is why.
2 - Or maybe they were just too rushed to get the API design correct. The Java 1.0 release was made under extreme pressure to meet a perceived market opportunity. A few mistakes were made and could not be corrected in time. Other examples include the Enumeration
API, the deprecated Thread
methods, the Hashtable
and Vector
classes, StringBuffer
and so on. But once Java 1.1 was released, it was too late.
1
They could have made theEmptyStackException
a subclass ofNoSuchElementException
. Sure, some code would have broken, but probably only very, very little code. (I say this because, for example, in python3 they decided to reorganize the exception hierarchy and they this a couple of these tricks to provide a minimum level of compatibility.)
– Giacomo Alzetta
Nov 30 at 13:01
Umm ... yes they could have. (It wouldn't break binary compatibility.) However, both exceptions existed in Java 1.0 so that change would have been "awkward" to explain. And since this is really a cosmetic issue it would have been difficult to justify from a pragmatic perspective.
– Stephen C
Nov 30 at 13:22
2
@GiacomoAlzetta, as you said, it would be a breaking change. Java tends to avoid them. Different languages, different politics...
– Guillaume
Nov 30 at 14:35
@GiacomoAlzetta I think your example of python3 is a great example of why the Java folks were hesitant to make that kind of a breaking change. It's been a decade since that came out, and python 2.x is still heavily used precisely because people don't want to deal with the breakage. Yes, theEmptyStackException
change would have been small. But there are a lot of such It'd-Be-Small changes you could imagine, and in aggregate, they become enough of a pain to make it not worthwhile to upgrade.
– yshavit
Nov 30 at 18:32
@yshavit The only major roadblock between Python 2 and 3 was the byte vs Unicode string issues. All the other stuff is trivially minor and hasn't impeded much, and tools exist to deal with those minor problems pretty much automatically. And much like Python, someone who can't afford to upgrade immediately can stick with the older version for a little while. That's part of why it's important to do this stuff regularly, so that people don't depend on old cruft for a decade. Python is a pretty great example of how major changes can be done smoothly. Just look at the division change.
– jpmc26
Nov 30 at 18:50
|
show 1 more comment
up vote
9
down vote
The Stack class was there first. In the Javadoc it says "since JDK 1". It defined its own exception type because … it could.
At that same time, the NoSuchElementException already existed, but the Java collections framework didn't exist yet. Therefore it was not yet common to use that exception widely. It was "just one of the predefined exception" types.
The collections framework was added in Java 1.2, and it could not use the StackEmptyException
because its name restricts it to be used only with stacks.
At that point, the old Stack class could not be modified anymore since that would have broken existing code. Java has been successful of being backwards compatible over decades, and the exception inconsistency is one sign of this compatibility.
To get an official answer, you could look at the code. It says:
@author Jonathan Payne
If it's really important for you to know, you can contact him directly and ask him whether he remembers what he did 20 years ago. Maybe he does. :)
3
True, but Stack's parent, Vector, was there in 1.0 and uses NoSuchElementException. Same with Enumeration (and it's child). So multiple (admittedly non-collections-framework) classes were already using NoSuchElementException
– Vasan
Nov 30 at 4:28
add a comment |
up vote
6
down vote
Queue
s have special methods which allow it to return null
instead of throw an exception. This is useful in the case of a BlockingQueue
, which should block until a value appears, or throw an exception.
The legacy class in this case - Stack
- is special in that it throws its own exception when it's empty. There's no real inconsistency here in that the exceptions are different, it's just that the collections are serving two completely different purposes. If nothing else, this is explicitly documented by the fact that the exceptions are different.
Do you mean thepoll
method?
– Hai Hoang
Nov 30 at 4:14
4
It is noteworthy though that many other collection and collection-esque classes use NoSuchElementException to indicate they're empty - everything from Enumeration to Stack's own superclass, Vector.
– Vasan
Nov 30 at 4:15
@HaiHoang:offer
,poll
andpeek
.
– Makoto
Nov 30 at 7:24
add a comment |
up vote
3
down vote
I guess there is no inconsistency or whatsoever confusion you have regarding this. Both the methods pop() and remove() are self-explanatory and extend RuntimeException. The exceptions are named as per proper conventions and stand themselves for explanation.
Queue-> remove()
throws NoSuchElementException. As per docs, remove()
Retrieves and removes the head of this queue. So, if the function performs only retrieval, it can return null and not throw Exception(Refer, poll()
). In addition to retrieval, it also tries to remove the head of queue and hence NoSuchElementException is thrown.
Stack -> pop()
throws EmptyStackException which means the stack is empty(It can also throw NoSuchElementException logically, but EmptyStackException is more clean and easy to debug the cause. Also, this Exception is thrown only by Stack class)
Coming to your exact answer, Stack is a class whereas Queue is an interface.
You can construct your own objects of Stack class, which means it can have Exceptions precisely for it.
But Queue, being an interface, relies on (LinkedList
,for example) for actual method declarations. Hence, if you plan to implement Queue<E>
by your own, you can have a EmptyQueueException
or anything you like. But if you rely on LinkedList
, it is simply not logical to expect an EmptyQueueException
from LinkedList.remove()
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
34
down vote
accepted
The Stack
class is a legacy class from the Java 1.0 days, prior to the introduction of the collections framework. Its interface has to be backwards compatible ... and that is how it was designed.
By contrast, the Queue
interface was introduced in the Java 1.5 revision of the collections framework. By that time, the NoSuchElementException
had been chosen by the designers as the best way to express this kind of error condition1.
Note that NoSuchElementException
could have been used in Stack
since both classes existed in Java 1.0, but clearly, the designers had other ideas back then2.
So this is just a historical anomaly that has arisen due to the way that the Java APIs have evolved. It cannot be fixed without breaking binary compatibility for existing applications that use the Stack
class.
1 - You may disagree with that, but you asked why, and this is why.
2 - Or maybe they were just too rushed to get the API design correct. The Java 1.0 release was made under extreme pressure to meet a perceived market opportunity. A few mistakes were made and could not be corrected in time. Other examples include the Enumeration
API, the deprecated Thread
methods, the Hashtable
and Vector
classes, StringBuffer
and so on. But once Java 1.1 was released, it was too late.
1
They could have made theEmptyStackException
a subclass ofNoSuchElementException
. Sure, some code would have broken, but probably only very, very little code. (I say this because, for example, in python3 they decided to reorganize the exception hierarchy and they this a couple of these tricks to provide a minimum level of compatibility.)
– Giacomo Alzetta
Nov 30 at 13:01
Umm ... yes they could have. (It wouldn't break binary compatibility.) However, both exceptions existed in Java 1.0 so that change would have been "awkward" to explain. And since this is really a cosmetic issue it would have been difficult to justify from a pragmatic perspective.
– Stephen C
Nov 30 at 13:22
2
@GiacomoAlzetta, as you said, it would be a breaking change. Java tends to avoid them. Different languages, different politics...
– Guillaume
Nov 30 at 14:35
@GiacomoAlzetta I think your example of python3 is a great example of why the Java folks were hesitant to make that kind of a breaking change. It's been a decade since that came out, and python 2.x is still heavily used precisely because people don't want to deal with the breakage. Yes, theEmptyStackException
change would have been small. But there are a lot of such It'd-Be-Small changes you could imagine, and in aggregate, they become enough of a pain to make it not worthwhile to upgrade.
– yshavit
Nov 30 at 18:32
@yshavit The only major roadblock between Python 2 and 3 was the byte vs Unicode string issues. All the other stuff is trivially minor and hasn't impeded much, and tools exist to deal with those minor problems pretty much automatically. And much like Python, someone who can't afford to upgrade immediately can stick with the older version for a little while. That's part of why it's important to do this stuff regularly, so that people don't depend on old cruft for a decade. Python is a pretty great example of how major changes can be done smoothly. Just look at the division change.
– jpmc26
Nov 30 at 18:50
|
show 1 more comment
up vote
34
down vote
accepted
The Stack
class is a legacy class from the Java 1.0 days, prior to the introduction of the collections framework. Its interface has to be backwards compatible ... and that is how it was designed.
By contrast, the Queue
interface was introduced in the Java 1.5 revision of the collections framework. By that time, the NoSuchElementException
had been chosen by the designers as the best way to express this kind of error condition1.
Note that NoSuchElementException
could have been used in Stack
since both classes existed in Java 1.0, but clearly, the designers had other ideas back then2.
So this is just a historical anomaly that has arisen due to the way that the Java APIs have evolved. It cannot be fixed without breaking binary compatibility for existing applications that use the Stack
class.
1 - You may disagree with that, but you asked why, and this is why.
2 - Or maybe they were just too rushed to get the API design correct. The Java 1.0 release was made under extreme pressure to meet a perceived market opportunity. A few mistakes were made and could not be corrected in time. Other examples include the Enumeration
API, the deprecated Thread
methods, the Hashtable
and Vector
classes, StringBuffer
and so on. But once Java 1.1 was released, it was too late.
1
They could have made theEmptyStackException
a subclass ofNoSuchElementException
. Sure, some code would have broken, but probably only very, very little code. (I say this because, for example, in python3 they decided to reorganize the exception hierarchy and they this a couple of these tricks to provide a minimum level of compatibility.)
– Giacomo Alzetta
Nov 30 at 13:01
Umm ... yes they could have. (It wouldn't break binary compatibility.) However, both exceptions existed in Java 1.0 so that change would have been "awkward" to explain. And since this is really a cosmetic issue it would have been difficult to justify from a pragmatic perspective.
– Stephen C
Nov 30 at 13:22
2
@GiacomoAlzetta, as you said, it would be a breaking change. Java tends to avoid them. Different languages, different politics...
– Guillaume
Nov 30 at 14:35
@GiacomoAlzetta I think your example of python3 is a great example of why the Java folks were hesitant to make that kind of a breaking change. It's been a decade since that came out, and python 2.x is still heavily used precisely because people don't want to deal with the breakage. Yes, theEmptyStackException
change would have been small. But there are a lot of such It'd-Be-Small changes you could imagine, and in aggregate, they become enough of a pain to make it not worthwhile to upgrade.
– yshavit
Nov 30 at 18:32
@yshavit The only major roadblock between Python 2 and 3 was the byte vs Unicode string issues. All the other stuff is trivially minor and hasn't impeded much, and tools exist to deal with those minor problems pretty much automatically. And much like Python, someone who can't afford to upgrade immediately can stick with the older version for a little while. That's part of why it's important to do this stuff regularly, so that people don't depend on old cruft for a decade. Python is a pretty great example of how major changes can be done smoothly. Just look at the division change.
– jpmc26
Nov 30 at 18:50
|
show 1 more comment
up vote
34
down vote
accepted
up vote
34
down vote
accepted
The Stack
class is a legacy class from the Java 1.0 days, prior to the introduction of the collections framework. Its interface has to be backwards compatible ... and that is how it was designed.
By contrast, the Queue
interface was introduced in the Java 1.5 revision of the collections framework. By that time, the NoSuchElementException
had been chosen by the designers as the best way to express this kind of error condition1.
Note that NoSuchElementException
could have been used in Stack
since both classes existed in Java 1.0, but clearly, the designers had other ideas back then2.
So this is just a historical anomaly that has arisen due to the way that the Java APIs have evolved. It cannot be fixed without breaking binary compatibility for existing applications that use the Stack
class.
1 - You may disagree with that, but you asked why, and this is why.
2 - Or maybe they were just too rushed to get the API design correct. The Java 1.0 release was made under extreme pressure to meet a perceived market opportunity. A few mistakes were made and could not be corrected in time. Other examples include the Enumeration
API, the deprecated Thread
methods, the Hashtable
and Vector
classes, StringBuffer
and so on. But once Java 1.1 was released, it was too late.
The Stack
class is a legacy class from the Java 1.0 days, prior to the introduction of the collections framework. Its interface has to be backwards compatible ... and that is how it was designed.
By contrast, the Queue
interface was introduced in the Java 1.5 revision of the collections framework. By that time, the NoSuchElementException
had been chosen by the designers as the best way to express this kind of error condition1.
Note that NoSuchElementException
could have been used in Stack
since both classes existed in Java 1.0, but clearly, the designers had other ideas back then2.
So this is just a historical anomaly that has arisen due to the way that the Java APIs have evolved. It cannot be fixed without breaking binary compatibility for existing applications that use the Stack
class.
1 - You may disagree with that, but you asked why, and this is why.
2 - Or maybe they were just too rushed to get the API design correct. The Java 1.0 release was made under extreme pressure to meet a perceived market opportunity. A few mistakes were made and could not be corrected in time. Other examples include the Enumeration
API, the deprecated Thread
methods, the Hashtable
and Vector
classes, StringBuffer
and so on. But once Java 1.1 was released, it was too late.
edited Nov 30 at 15:59
isanae
2,49211335
2,49211335
answered Nov 30 at 4:37
Stephen C
510k69559910
510k69559910
1
They could have made theEmptyStackException
a subclass ofNoSuchElementException
. Sure, some code would have broken, but probably only very, very little code. (I say this because, for example, in python3 they decided to reorganize the exception hierarchy and they this a couple of these tricks to provide a minimum level of compatibility.)
– Giacomo Alzetta
Nov 30 at 13:01
Umm ... yes they could have. (It wouldn't break binary compatibility.) However, both exceptions existed in Java 1.0 so that change would have been "awkward" to explain. And since this is really a cosmetic issue it would have been difficult to justify from a pragmatic perspective.
– Stephen C
Nov 30 at 13:22
2
@GiacomoAlzetta, as you said, it would be a breaking change. Java tends to avoid them. Different languages, different politics...
– Guillaume
Nov 30 at 14:35
@GiacomoAlzetta I think your example of python3 is a great example of why the Java folks were hesitant to make that kind of a breaking change. It's been a decade since that came out, and python 2.x is still heavily used precisely because people don't want to deal with the breakage. Yes, theEmptyStackException
change would have been small. But there are a lot of such It'd-Be-Small changes you could imagine, and in aggregate, they become enough of a pain to make it not worthwhile to upgrade.
– yshavit
Nov 30 at 18:32
@yshavit The only major roadblock between Python 2 and 3 was the byte vs Unicode string issues. All the other stuff is trivially minor and hasn't impeded much, and tools exist to deal with those minor problems pretty much automatically. And much like Python, someone who can't afford to upgrade immediately can stick with the older version for a little while. That's part of why it's important to do this stuff regularly, so that people don't depend on old cruft for a decade. Python is a pretty great example of how major changes can be done smoothly. Just look at the division change.
– jpmc26
Nov 30 at 18:50
|
show 1 more comment
1
They could have made theEmptyStackException
a subclass ofNoSuchElementException
. Sure, some code would have broken, but probably only very, very little code. (I say this because, for example, in python3 they decided to reorganize the exception hierarchy and they this a couple of these tricks to provide a minimum level of compatibility.)
– Giacomo Alzetta
Nov 30 at 13:01
Umm ... yes they could have. (It wouldn't break binary compatibility.) However, both exceptions existed in Java 1.0 so that change would have been "awkward" to explain. And since this is really a cosmetic issue it would have been difficult to justify from a pragmatic perspective.
– Stephen C
Nov 30 at 13:22
2
@GiacomoAlzetta, as you said, it would be a breaking change. Java tends to avoid them. Different languages, different politics...
– Guillaume
Nov 30 at 14:35
@GiacomoAlzetta I think your example of python3 is a great example of why the Java folks were hesitant to make that kind of a breaking change. It's been a decade since that came out, and python 2.x is still heavily used precisely because people don't want to deal with the breakage. Yes, theEmptyStackException
change would have been small. But there are a lot of such It'd-Be-Small changes you could imagine, and in aggregate, they become enough of a pain to make it not worthwhile to upgrade.
– yshavit
Nov 30 at 18:32
@yshavit The only major roadblock between Python 2 and 3 was the byte vs Unicode string issues. All the other stuff is trivially minor and hasn't impeded much, and tools exist to deal with those minor problems pretty much automatically. And much like Python, someone who can't afford to upgrade immediately can stick with the older version for a little while. That's part of why it's important to do this stuff regularly, so that people don't depend on old cruft for a decade. Python is a pretty great example of how major changes can be done smoothly. Just look at the division change.
– jpmc26
Nov 30 at 18:50
1
1
They could have made the
EmptyStackException
a subclass of NoSuchElementException
. Sure, some code would have broken, but probably only very, very little code. (I say this because, for example, in python3 they decided to reorganize the exception hierarchy and they this a couple of these tricks to provide a minimum level of compatibility.)– Giacomo Alzetta
Nov 30 at 13:01
They could have made the
EmptyStackException
a subclass of NoSuchElementException
. Sure, some code would have broken, but probably only very, very little code. (I say this because, for example, in python3 they decided to reorganize the exception hierarchy and they this a couple of these tricks to provide a minimum level of compatibility.)– Giacomo Alzetta
Nov 30 at 13:01
Umm ... yes they could have. (It wouldn't break binary compatibility.) However, both exceptions existed in Java 1.0 so that change would have been "awkward" to explain. And since this is really a cosmetic issue it would have been difficult to justify from a pragmatic perspective.
– Stephen C
Nov 30 at 13:22
Umm ... yes they could have. (It wouldn't break binary compatibility.) However, both exceptions existed in Java 1.0 so that change would have been "awkward" to explain. And since this is really a cosmetic issue it would have been difficult to justify from a pragmatic perspective.
– Stephen C
Nov 30 at 13:22
2
2
@GiacomoAlzetta, as you said, it would be a breaking change. Java tends to avoid them. Different languages, different politics...
– Guillaume
Nov 30 at 14:35
@GiacomoAlzetta, as you said, it would be a breaking change. Java tends to avoid them. Different languages, different politics...
– Guillaume
Nov 30 at 14:35
@GiacomoAlzetta I think your example of python3 is a great example of why the Java folks were hesitant to make that kind of a breaking change. It's been a decade since that came out, and python 2.x is still heavily used precisely because people don't want to deal with the breakage. Yes, the
EmptyStackException
change would have been small. But there are a lot of such It'd-Be-Small changes you could imagine, and in aggregate, they become enough of a pain to make it not worthwhile to upgrade.– yshavit
Nov 30 at 18:32
@GiacomoAlzetta I think your example of python3 is a great example of why the Java folks were hesitant to make that kind of a breaking change. It's been a decade since that came out, and python 2.x is still heavily used precisely because people don't want to deal with the breakage. Yes, the
EmptyStackException
change would have been small. But there are a lot of such It'd-Be-Small changes you could imagine, and in aggregate, they become enough of a pain to make it not worthwhile to upgrade.– yshavit
Nov 30 at 18:32
@yshavit The only major roadblock between Python 2 and 3 was the byte vs Unicode string issues. All the other stuff is trivially minor and hasn't impeded much, and tools exist to deal with those minor problems pretty much automatically. And much like Python, someone who can't afford to upgrade immediately can stick with the older version for a little while. That's part of why it's important to do this stuff regularly, so that people don't depend on old cruft for a decade. Python is a pretty great example of how major changes can be done smoothly. Just look at the division change.
– jpmc26
Nov 30 at 18:50
@yshavit The only major roadblock between Python 2 and 3 was the byte vs Unicode string issues. All the other stuff is trivially minor and hasn't impeded much, and tools exist to deal with those minor problems pretty much automatically. And much like Python, someone who can't afford to upgrade immediately can stick with the older version for a little while. That's part of why it's important to do this stuff regularly, so that people don't depend on old cruft for a decade. Python is a pretty great example of how major changes can be done smoothly. Just look at the division change.
– jpmc26
Nov 30 at 18:50
|
show 1 more comment
up vote
9
down vote
The Stack class was there first. In the Javadoc it says "since JDK 1". It defined its own exception type because … it could.
At that same time, the NoSuchElementException already existed, but the Java collections framework didn't exist yet. Therefore it was not yet common to use that exception widely. It was "just one of the predefined exception" types.
The collections framework was added in Java 1.2, and it could not use the StackEmptyException
because its name restricts it to be used only with stacks.
At that point, the old Stack class could not be modified anymore since that would have broken existing code. Java has been successful of being backwards compatible over decades, and the exception inconsistency is one sign of this compatibility.
To get an official answer, you could look at the code. It says:
@author Jonathan Payne
If it's really important for you to know, you can contact him directly and ask him whether he remembers what he did 20 years ago. Maybe he does. :)
3
True, but Stack's parent, Vector, was there in 1.0 and uses NoSuchElementException. Same with Enumeration (and it's child). So multiple (admittedly non-collections-framework) classes were already using NoSuchElementException
– Vasan
Nov 30 at 4:28
add a comment |
up vote
9
down vote
The Stack class was there first. In the Javadoc it says "since JDK 1". It defined its own exception type because … it could.
At that same time, the NoSuchElementException already existed, but the Java collections framework didn't exist yet. Therefore it was not yet common to use that exception widely. It was "just one of the predefined exception" types.
The collections framework was added in Java 1.2, and it could not use the StackEmptyException
because its name restricts it to be used only with stacks.
At that point, the old Stack class could not be modified anymore since that would have broken existing code. Java has been successful of being backwards compatible over decades, and the exception inconsistency is one sign of this compatibility.
To get an official answer, you could look at the code. It says:
@author Jonathan Payne
If it's really important for you to know, you can contact him directly and ask him whether he remembers what he did 20 years ago. Maybe he does. :)
3
True, but Stack's parent, Vector, was there in 1.0 and uses NoSuchElementException. Same with Enumeration (and it's child). So multiple (admittedly non-collections-framework) classes were already using NoSuchElementException
– Vasan
Nov 30 at 4:28
add a comment |
up vote
9
down vote
up vote
9
down vote
The Stack class was there first. In the Javadoc it says "since JDK 1". It defined its own exception type because … it could.
At that same time, the NoSuchElementException already existed, but the Java collections framework didn't exist yet. Therefore it was not yet common to use that exception widely. It was "just one of the predefined exception" types.
The collections framework was added in Java 1.2, and it could not use the StackEmptyException
because its name restricts it to be used only with stacks.
At that point, the old Stack class could not be modified anymore since that would have broken existing code. Java has been successful of being backwards compatible over decades, and the exception inconsistency is one sign of this compatibility.
To get an official answer, you could look at the code. It says:
@author Jonathan Payne
If it's really important for you to know, you can contact him directly and ask him whether he remembers what he did 20 years ago. Maybe he does. :)
The Stack class was there first. In the Javadoc it says "since JDK 1". It defined its own exception type because … it could.
At that same time, the NoSuchElementException already existed, but the Java collections framework didn't exist yet. Therefore it was not yet common to use that exception widely. It was "just one of the predefined exception" types.
The collections framework was added in Java 1.2, and it could not use the StackEmptyException
because its name restricts it to be used only with stacks.
At that point, the old Stack class could not be modified anymore since that would have broken existing code. Java has been successful of being backwards compatible over decades, and the exception inconsistency is one sign of this compatibility.
To get an official answer, you could look at the code. It says:
@author Jonathan Payne
If it's really important for you to know, you can contact him directly and ask him whether he remembers what he did 20 years ago. Maybe he does. :)
edited Nov 30 at 20:13
answered Nov 30 at 4:24
Roland Illig
29.2k95890
29.2k95890
3
True, but Stack's parent, Vector, was there in 1.0 and uses NoSuchElementException. Same with Enumeration (and it's child). So multiple (admittedly non-collections-framework) classes were already using NoSuchElementException
– Vasan
Nov 30 at 4:28
add a comment |
3
True, but Stack's parent, Vector, was there in 1.0 and uses NoSuchElementException. Same with Enumeration (and it's child). So multiple (admittedly non-collections-framework) classes were already using NoSuchElementException
– Vasan
Nov 30 at 4:28
3
3
True, but Stack's parent, Vector, was there in 1.0 and uses NoSuchElementException. Same with Enumeration (and it's child). So multiple (admittedly non-collections-framework) classes were already using NoSuchElementException
– Vasan
Nov 30 at 4:28
True, but Stack's parent, Vector, was there in 1.0 and uses NoSuchElementException. Same with Enumeration (and it's child). So multiple (admittedly non-collections-framework) classes were already using NoSuchElementException
– Vasan
Nov 30 at 4:28
add a comment |
up vote
6
down vote
Queue
s have special methods which allow it to return null
instead of throw an exception. This is useful in the case of a BlockingQueue
, which should block until a value appears, or throw an exception.
The legacy class in this case - Stack
- is special in that it throws its own exception when it's empty. There's no real inconsistency here in that the exceptions are different, it's just that the collections are serving two completely different purposes. If nothing else, this is explicitly documented by the fact that the exceptions are different.
Do you mean thepoll
method?
– Hai Hoang
Nov 30 at 4:14
4
It is noteworthy though that many other collection and collection-esque classes use NoSuchElementException to indicate they're empty - everything from Enumeration to Stack's own superclass, Vector.
– Vasan
Nov 30 at 4:15
@HaiHoang:offer
,poll
andpeek
.
– Makoto
Nov 30 at 7:24
add a comment |
up vote
6
down vote
Queue
s have special methods which allow it to return null
instead of throw an exception. This is useful in the case of a BlockingQueue
, which should block until a value appears, or throw an exception.
The legacy class in this case - Stack
- is special in that it throws its own exception when it's empty. There's no real inconsistency here in that the exceptions are different, it's just that the collections are serving two completely different purposes. If nothing else, this is explicitly documented by the fact that the exceptions are different.
Do you mean thepoll
method?
– Hai Hoang
Nov 30 at 4:14
4
It is noteworthy though that many other collection and collection-esque classes use NoSuchElementException to indicate they're empty - everything from Enumeration to Stack's own superclass, Vector.
– Vasan
Nov 30 at 4:15
@HaiHoang:offer
,poll
andpeek
.
– Makoto
Nov 30 at 7:24
add a comment |
up vote
6
down vote
up vote
6
down vote
Queue
s have special methods which allow it to return null
instead of throw an exception. This is useful in the case of a BlockingQueue
, which should block until a value appears, or throw an exception.
The legacy class in this case - Stack
- is special in that it throws its own exception when it's empty. There's no real inconsistency here in that the exceptions are different, it's just that the collections are serving two completely different purposes. If nothing else, this is explicitly documented by the fact that the exceptions are different.
Queue
s have special methods which allow it to return null
instead of throw an exception. This is useful in the case of a BlockingQueue
, which should block until a value appears, or throw an exception.
The legacy class in this case - Stack
- is special in that it throws its own exception when it's empty. There's no real inconsistency here in that the exceptions are different, it's just that the collections are serving two completely different purposes. If nothing else, this is explicitly documented by the fact that the exceptions are different.
answered Nov 30 at 4:11
Makoto
79.8k15125167
79.8k15125167
Do you mean thepoll
method?
– Hai Hoang
Nov 30 at 4:14
4
It is noteworthy though that many other collection and collection-esque classes use NoSuchElementException to indicate they're empty - everything from Enumeration to Stack's own superclass, Vector.
– Vasan
Nov 30 at 4:15
@HaiHoang:offer
,poll
andpeek
.
– Makoto
Nov 30 at 7:24
add a comment |
Do you mean thepoll
method?
– Hai Hoang
Nov 30 at 4:14
4
It is noteworthy though that many other collection and collection-esque classes use NoSuchElementException to indicate they're empty - everything from Enumeration to Stack's own superclass, Vector.
– Vasan
Nov 30 at 4:15
@HaiHoang:offer
,poll
andpeek
.
– Makoto
Nov 30 at 7:24
Do you mean the
poll
method?– Hai Hoang
Nov 30 at 4:14
Do you mean the
poll
method?– Hai Hoang
Nov 30 at 4:14
4
4
It is noteworthy though that many other collection and collection-esque classes use NoSuchElementException to indicate they're empty - everything from Enumeration to Stack's own superclass, Vector.
– Vasan
Nov 30 at 4:15
It is noteworthy though that many other collection and collection-esque classes use NoSuchElementException to indicate they're empty - everything from Enumeration to Stack's own superclass, Vector.
– Vasan
Nov 30 at 4:15
@HaiHoang:
offer
, poll
and peek
.– Makoto
Nov 30 at 7:24
@HaiHoang:
offer
, poll
and peek
.– Makoto
Nov 30 at 7:24
add a comment |
up vote
3
down vote
I guess there is no inconsistency or whatsoever confusion you have regarding this. Both the methods pop() and remove() are self-explanatory and extend RuntimeException. The exceptions are named as per proper conventions and stand themselves for explanation.
Queue-> remove()
throws NoSuchElementException. As per docs, remove()
Retrieves and removes the head of this queue. So, if the function performs only retrieval, it can return null and not throw Exception(Refer, poll()
). In addition to retrieval, it also tries to remove the head of queue and hence NoSuchElementException is thrown.
Stack -> pop()
throws EmptyStackException which means the stack is empty(It can also throw NoSuchElementException logically, but EmptyStackException is more clean and easy to debug the cause. Also, this Exception is thrown only by Stack class)
Coming to your exact answer, Stack is a class whereas Queue is an interface.
You can construct your own objects of Stack class, which means it can have Exceptions precisely for it.
But Queue, being an interface, relies on (LinkedList
,for example) for actual method declarations. Hence, if you plan to implement Queue<E>
by your own, you can have a EmptyQueueException
or anything you like. But if you rely on LinkedList
, it is simply not logical to expect an EmptyQueueException
from LinkedList.remove()
add a comment |
up vote
3
down vote
I guess there is no inconsistency or whatsoever confusion you have regarding this. Both the methods pop() and remove() are self-explanatory and extend RuntimeException. The exceptions are named as per proper conventions and stand themselves for explanation.
Queue-> remove()
throws NoSuchElementException. As per docs, remove()
Retrieves and removes the head of this queue. So, if the function performs only retrieval, it can return null and not throw Exception(Refer, poll()
). In addition to retrieval, it also tries to remove the head of queue and hence NoSuchElementException is thrown.
Stack -> pop()
throws EmptyStackException which means the stack is empty(It can also throw NoSuchElementException logically, but EmptyStackException is more clean and easy to debug the cause. Also, this Exception is thrown only by Stack class)
Coming to your exact answer, Stack is a class whereas Queue is an interface.
You can construct your own objects of Stack class, which means it can have Exceptions precisely for it.
But Queue, being an interface, relies on (LinkedList
,for example) for actual method declarations. Hence, if you plan to implement Queue<E>
by your own, you can have a EmptyQueueException
or anything you like. But if you rely on LinkedList
, it is simply not logical to expect an EmptyQueueException
from LinkedList.remove()
add a comment |
up vote
3
down vote
up vote
3
down vote
I guess there is no inconsistency or whatsoever confusion you have regarding this. Both the methods pop() and remove() are self-explanatory and extend RuntimeException. The exceptions are named as per proper conventions and stand themselves for explanation.
Queue-> remove()
throws NoSuchElementException. As per docs, remove()
Retrieves and removes the head of this queue. So, if the function performs only retrieval, it can return null and not throw Exception(Refer, poll()
). In addition to retrieval, it also tries to remove the head of queue and hence NoSuchElementException is thrown.
Stack -> pop()
throws EmptyStackException which means the stack is empty(It can also throw NoSuchElementException logically, but EmptyStackException is more clean and easy to debug the cause. Also, this Exception is thrown only by Stack class)
Coming to your exact answer, Stack is a class whereas Queue is an interface.
You can construct your own objects of Stack class, which means it can have Exceptions precisely for it.
But Queue, being an interface, relies on (LinkedList
,for example) for actual method declarations. Hence, if you plan to implement Queue<E>
by your own, you can have a EmptyQueueException
or anything you like. But if you rely on LinkedList
, it is simply not logical to expect an EmptyQueueException
from LinkedList.remove()
I guess there is no inconsistency or whatsoever confusion you have regarding this. Both the methods pop() and remove() are self-explanatory and extend RuntimeException. The exceptions are named as per proper conventions and stand themselves for explanation.
Queue-> remove()
throws NoSuchElementException. As per docs, remove()
Retrieves and removes the head of this queue. So, if the function performs only retrieval, it can return null and not throw Exception(Refer, poll()
). In addition to retrieval, it also tries to remove the head of queue and hence NoSuchElementException is thrown.
Stack -> pop()
throws EmptyStackException which means the stack is empty(It can also throw NoSuchElementException logically, but EmptyStackException is more clean and easy to debug the cause. Also, this Exception is thrown only by Stack class)
Coming to your exact answer, Stack is a class whereas Queue is an interface.
You can construct your own objects of Stack class, which means it can have Exceptions precisely for it.
But Queue, being an interface, relies on (LinkedList
,for example) for actual method declarations. Hence, if you plan to implement Queue<E>
by your own, you can have a EmptyQueueException
or anything you like. But if you rely on LinkedList
, it is simply not logical to expect an EmptyQueueException
from LinkedList.remove()
edited Nov 30 at 4:36
answered Nov 30 at 4:16
Mohamed Anees A
593413
593413
add a comment |
add a comment |