java - Return only if no Exception is thrown -


i'm implementing fixed sized queue in java uses constant size arraylist underlying container, front() method supposed return front element of queue .

   public t front(){           try{             if(isempty())                 throw new exception("queue empty- can't return front element.");             return arraylist.get(frontindex);          }catch (exception e){             system.out.println(e);         }      } 

by coding in above way , want front() return value if no exception thrown ,however expected compiler show me "missing return statement." , there way can make function return if no exception thrown.

since catching exception in code compiler shows missing return statement error.

you can implement function :

public t front() throws exception {      if(isempty()) {        throw new exception("queue empty- can't return front element.");     }      return arraylist.get(frontindex); } 

and handle exception @ calling function/client


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -