9

So my task may sound simple, but it has me boggled. I have looked through code on the internet, but I cannot get to grips with. Neither can I get to grips with the slides my teacher posted. This is what is required from me.

Create a new runtime exception type called EmptyStackException.

However I have no clue how to make the method, class, variable or whatever needs to be made in order to fulfill the requirement. I have a few classes that are implementations of a DynamicArrayStack and a LinkedStack. An interface for Stack.

Any pointers would be mightily helpful.

Thanks

Mjall2

Mjall2
  • 253
  • 1
  • 9
  • 24

3 Answers3

18

Create a new runtime exception type called EmptyStackException.

create type is done by

public class EmptyStackException extends RuntimeException { ... }

Now if only we knew what to put in this new type (a.k.a. class). Typically we look at the methods in the superclass and override those needing different handling. Below I have overridden some of them but delegated back to the existing class. No need to do this if you don't need to make any changes in behavior.

public class EmptyStackException extends RuntimeException {
      public EmptyStackException() {
          super();
      }
      public EmptyStackException(String s) {
          super(s);
      }
      public EmptyStackException(String s, Throwable throwable) {
          super(s, throwable);
      }
      public EmptyStackException(Throwable throwable) {
          super(throwable);
      }
    }
Jim
  • 1,161
  • 9
  • 21
  • Calling super(), calls the constructor of RuntimeException, which throws a run time exception! – Mjall2 Mar 21 '12 at 23:41
  • 2
    Calling super() should *construct* a RuntimeException, it does *not throw* an exception. – Jim Mar 21 '12 at 23:43
  • right but when we want to throw one, throw new EmptyStackException(); This will throw a runtimeexception i believe? – Mjall2 Mar 21 '12 at 23:48
  • throw new EmptyStackException(); will throw one yes. But, as indicated in my answer it is very uncommon to use a RuntimeException as custom Exception in case a stack is empty. You should extend Exception instead in order to write a custom, checked exception. – Matthew Mar 21 '12 at 23:56
  • what would the difference be?, i just see your code has a message – Mjall2 Mar 22 '12 at 00:01
  • The difference is, that RuntimeExceptions are not need to be declared when they are thrown this means they indicate programming errors. Exceptions on the other hand need to be declared in order to indicate that something went wrong (in your case, the stack is empty). It is important to notice that RuntimeException is a specialization of Exception. Generally spoken, exceptions are nothing more than a Message indicating that something went wrong. But, developers can react when they realize that a Exception has been thrown. Please read the Tutorial for more understanding of what exceptions are. – Matthew Mar 22 '12 at 00:16
5

In order to do so, you have to extend the class RuntimeException.

There are two types of Exceptions in Java: unchecked and checked exceptions. RuntimeExceptions are of the second type. This means they do not need to be explicitly handled and declared.

Normally, one uses checked exceptions when writing custom exceptions. This is done by extending the class Exception. I do not see any use-case for creating a custom RuntimeException.

Anyway, the following code shows how to write your own RuntimeException:

public class EmptyStackException extends RuntimeException{

   public EmptyStackException(String message){
      super(message);
   }

}

From within your source code you could use this by the following statement:

throw new EmptyStackException("Stack was Empty, can't pop");

For more information regarding exceptions i recommend you the following Tutorial

Matthew
  • 816
  • 1
  • 7
  • 10
1

Sounds like you may want to read up on Java. Check out The Java Tutorials, especially this one about Exceptions.


To put it simply, exceptions are a special kind of object representing an event outside of the normal operation of your code, causing control flow to be subverted. For example, an ArrayIndexOutOfBoundsException means your code tried to index to a position in an array that did not exist, such as -1.

Because of their association with bugs, exceptions often have a bad connotation to newer programmers. But because Java is Object Oriented, you can extend RuntimeException to create your own, custom exception types, which is quite useful for debugging and code clarity. To throw a custom exception while your code is executing, you'll have to

  1. have defined the custom exception,
  2. detect the exceptional condition, and
  3. throw the exception.

The easy way to define your custom RuntimeException is to define a class like:

public EmptyStackException extends RuntimeException {
    // customize error messages if necessay
}

Then you'd detect and throw the Exception like:

if (/** stack is empty */) {
    throw new EmptyStackException();
}

These are just the basics. You can also define custom exceptions on the fly. Hope this helps!

Chandra Sekhar
  • 16,256
  • 10
  • 67
  • 90
calebds
  • 25,670
  • 9
  • 46
  • 74