Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    [JAVA] Eccezioni che non vanno!!!

    Ciao,
    ho questo codice (con due eccezioni definite da me.. solo costruttori ereditati da Objct) Ma quando faccio il test mi da questo errore:
    codice:
    stack/TestStack.java [28:1] exception stack.Exception.EmptyStackException is never thrown in body of corresponding try statement
           } catch (EmptyStackException e) {
             ^
    1 error
    Errors compiling TestStack.
    --- CODICE STACK CLASS --
    codice:
    /*
     * Stack.java
     *
     * Created on April 4, 2004, 5:58 PM
     */
    
    /**
     *
     * @author  root
     */
    package stack;
    import stack.Exception.*;
    
    public class Stacoone
    {
        private int stacksize;
        private int curr;
        private Object[] stackval;
        
        /** Creates a new instance of Stack */
        public Stacoone (int s)
        {
            stackval = new Object[s];
            curr=-1;
            stacksize=s;
        }
        
        public void push (Object o) {
            if (stacksize-1 == curr) throw new FullStackException();
           
            curr++;
            stackval[curr] = o;
            
            
            
        }
        
        public Object pop () {
            if (curr==-1) throw new EmptyStackException();
            
            return stackval[curr--];
    
        }
    
        public Object top () {
            if (curr == -1) throw new EmptyStackException();
            
            return stackval[stacksize];
    
        }
        
        public int size () {
            return curr+1;
        }
        
        public boolean isEmpty() { return (curr == -1); }
        
    }
    -- CODICE MAIN --
    codice:
    /*
     * TestStack.java
     *
     * Created on April 4, 2004, 6:24 PM
     */
    
    /**
     *
     * @author  root
     */
    package stack;
    import stack.Stacoone;
    import stack.Exception.*;
    //import java.util.*;
    
    public class TestStack
    {
        
        /**
         * @param args the command line arguments
         */
        public static void main (String[] args) throws FullStackException, EmptyStackException
        {
            
            Stacoone interi = new Stacoone(4);
           try {
            interi.pop();
           } catch (EmptyStackException e) {
           System.out.println("Stack vuoto");
           }
          
            //interi.push(new Integer(3));
           
            // TODO code application logic here
        }
    Grazie ciao!

  2. #2
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    1,798
    Ho dato un'occhiata veloce:
    [code]
    public void push (Object o) throws FullStackException{
    if (stacksize-1 == curr) throw new FullStackException();
    [code]

    nella firma del metodo aggiungi "throws FullStackException".
    In modo similare correggi le altre parti di codice.
    Slack? Smack!

  3. #3
    Originariamente inviato da Sergio Pedone
    Ho dato un'occhiata veloce:
    [code]
    public void push (Object o) throws FullStackException{
    if (stacksize-1 == curr) throw new FullStackException();
    [code]

    nella firma del metodo aggiungi "throws FullStackException".
    In modo similare correggi le altre parti di codice.

    Grazie funziona!!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.