Visualizzazione dei risultati da 1 a 5 su 5

Discussione: Java Exception

  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2010
    Messaggi
    34

    Java Exception

    Salve a tutti, come si può realizzare una Exception personalizzata per esempio per segnalare all'utente di aver inserito due volte uno stesso valore?

    Grazie

  2. #2
    DoubleInsertException.java

    codice:
    package org.gengi.exceptions;
    
    public class DoubleInsertException extends Exception{
    
        private Object value;
    
        public DoubleInsertException(Object value) {
            this.value = value;
        }
    
        @Override
        public String toString() {
            return "DoubleInsertExecption: double insert of value " + value.toString();
        }
    }
    Demo:

    codice:
    package org.gengi.exceptions;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Demo {
    
        static List<Integer> values = new ArrayList<Integer>();
    
        static void insert(int number) throws DoubleInsertException{
    
            if(values.contains(number)){
    
                throw new DoubleInsertException(number);
            }
    
            values.add(number);
        }
    
        public static void main(String[] args){
            try {
                insert(1);
                insert(1);
            }
    
            catch (DoubleInsertException ex) {
                ex.printStackTrace();
            }
        }
    }
    ecco a te.

    Gengi

  3. #3
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da Gengi
    codice:
    public class DoubleInsertException extends Exception{
    
        private Object value;
    
        public DoubleInsertException(Object value) {
            this.value = value;
        }
    
        @Override
        public String toString() {
            return "DoubleInsertExecption: double insert of value " + value.toString();
        }
    }
    A dire il vero sarebbe meglio sfruttare il "message" (getMessage() di Throwable) nella eccezione, anche perché oltretutto toString() in Throwable è già implementato per comporre la stringa con il tipo eccezione + messaggio.

    Quindi semplicemente:

    codice:
    public class DoubleInsertException extends Exception {
        public DoubleInsertException(Object value) {
            super("double insert of value " + value);
        }
    }
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  4. #4
    Ma sii, un esempio così, non sto mica a guardare tutto il concetto è quello

  5. #5
    Utente di HTML.it
    Registrato dal
    Jun 2010
    Messaggi
    34
    Ok. grazie, concetto afferrato!

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.