Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2007
    Messaggi
    561

    [Java] come gestire una eccezione

    Nel codice che posto sotto ho un problema. Ci sono due righe che possono generare entrambe delle eccezioni IOException. Le righe sono socket=new Socket(InetAddress.getByName("localhost"),f.getPor t()) e output=new ObjectOutputStream(socket.getOutputStream()). Ecco mi servirebbe un modo per distinguere i due errori. Io pensavo che ogni eccezione fosse individuata da un codice e per questo ho messo un if nel blocco Exception per fare il controllo, ma ho controllato e non è questo il modo.

    public void runClient(GUI frame){
    FileConfig f=new FileConfig();
    try{
    frame.writeTextArea("Connessione con il server...");
    f.readProperties();
    socket=new Socket(InetAddress.getByName("localhost"),f.getPor t());
    frame.writeTextArea("Connessione effettuata con successo");
    output=new ObjectOutputStream(socket.getOutputStream());
    output.flush();
    output.flush();
    input=new ObjectInputStream(socket.getInputStream());
    String x=(String)input.readObject();
    if(x.equals("OK"))
    autorizzazione=true;
    }catch (IOException e) {
    System.err.println(());
    if(e.hashCode()==281)
    frame.writeTextArea("Connessione rifiutata");
    else if(e.hashCode()==275)
    System.err.println(e.getMessage());
    }catch(Exception eof){
    System.err.println(eof.getMessage());
    }

    }


    tulipan

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    GUI che cosa è? E runClient() in quale thread lo stai eseguendo? Nel contesto del EDT, event-dispatch-thread (e se così è sbagliato come approccio)?? O in un altro tuo thread creato appositamente?
    Insomma ... ci sono svariate questioni, relative anche ai thread.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    racchiudi ciascuna istruzione in un try/catch. Così sai quale stai gestendo...
    Ivan Venuti
    Vuoi scaricare alcuni articoli sulla programmazione, pubblicati su riviste di informatica? Visita http://ivenuti.altervista.org/articoli.htm. Se vuoi imparare JavaScript leggi il mio libro: http://www.fag.it/scheda.aspx?ID=21754 !

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.