0

This is my array implementation of stack code:

public class ArrayStack2{ 
    private final int DEFAULT_SIZE=10;
    public int tos;
    Object[] array;
    public ArrayStack2(){
        array   =new Object[DEFAULT_SIZE];
            tos=-1;
    }
    public void push(Object e){
        try{
            array[tos+1]=e;
            tos++;
            }
        catch(OverFlowException e1){
            e1.print();
        }

    }  

and this is OverFlowException class:

public class OverFlowException extends Exception{
    public OverFlowException(){
        super();
    }
    public OverFlowException(String s){
        super(s);

    }
    public void print(){
        System.out.println("OverFlow");
    }
}  

When I run this compiler gives the error "exception OverFlowException is never thrown in body of corresponding try statement "
then I realized I have not included the part to check if the array isFull().
My question is I have isFull() method in ArrayStack2 class and how can i call it from OverFlowException class.
Please help me to find a way to correct this exception problem

clarkson
  • 561
  • 2
  • 16
  • 32

2 Answers2

0

Do what the error says to do. That exception can never be caught because it has no possibility to be thrown. You need to throw it if something bad happens:

    try{
        array[tos+1]=e;
        tos++;
        if (thereWasAnOverflow) // or however you want to test it. isFull() maybe
            throw new OverFlowException();
    }
    catch(OverFlowException e1){
        e1.print();
    }
BitNinja
  • 1,477
  • 1
  • 19
  • 25
  • Then what should OverFlowException class should include?I thought how try and catch works is if there's no error try part is executed.And if there's a error catch clause is executed.When catch is executed it looks at the OverFlowException class.I thought there I should have to mention how to test whether an error has occured(like calling for is full).I don't understand how this try and catch works.Can you please explain it to me – clarkson May 10 '14 at 19:01
  • @clarkson Your logic is correct for the most part. But, custom exception classes are not responsible for knowing when to throw itself. That's why you have to throw it. – BitNinja May 10 '14 at 19:06
0

User defined exception is unchecked exception only if it extends RuntimeException.

In your case it OverFlowException is a checked exception and compiler checks for its possibilities. If there is no possibilities of throwing this checked exception then there is no meaning of handling a checked exception and compiler threat it as Unreachable catch block.

In JAVA anything that is not reachable will result in compile time error.


Try with this sample code for well known checked exception

try {
    System.out.println("hello");
} catch (IOException e) {
    e.printStackTrace();
}

Compile time error:

Unreachable catch block for IOException. 
This exception is never thrown from the try statement body

For more info have a look at User defined exception are checked or unchecked exceptions


My question is I have isFull() method in ArrayStack2 class and how can i call it from OverFlowException class.

Do it in reverse order means throw a new object of OverFlowException class from isFull() method in ArrayStack2 class based on your condition.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • I have thrown the exception as `public static void main (String args[]) throws EmptyStackException,OverFlowException {`.Even so it gives that error – clarkson May 10 '14 at 18:53
  • doesn't matter because you have handled it in catch block where it's never thrown – Braj May 10 '14 at 18:55