Sventrip (Normal User)
Newbie
Messaggi: 19
Iscritto: 20/07/2008
|
Sto ripassando le eccezioni e nel libro da dove studio ho provato a fare un esercizio dove chiede di creare una classe con il main(), un metodo reverse() che prende come argomento una stringa e ritorna la stringa al contrario, dice anche che se si passa la stringa al metodo con lunghezza 0 si presenta una eccezione che devo poi arrangiare(questo lo scopo), ma a me non mi da nessuna eccezione passando una stringa di length 0, ce qualcosa che mi sfugge , illuminatemi
Codice sorgente - presumibilmente Java |
public class Propagate { public static void main (String args []) { reverse(r); } for (int i= s.length()-1; i>=0; --i){ reverseStr += s.charAt(i); } return reverseStr; } }
|
Ultima modifica effettuata da Sventrip il 14/01/2010 alle 18:27 |
|
nessuno (Normal User)
Guru^2
Messaggi: 6402
Iscritto: 03/01/2010
|
Devi essere tu a controllare prima di usare la stringa, la sua lunghezza. E se è zero devi generare una exception
Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
---
Il grande studioso italiano Bruno de Finetti ( uno dei padri fondatori del moderno Calcolo delle probabilità ) chiamava il gioco del Lotto Tassa sulla stupidità. |
|
Sventrip (Normal User)
Newbie
Messaggi: 19
Iscritto: 20/07/2008
|
capisco ma non mi e' chiaro del tutto , perche la dichirazione delle eccezioni non e' ancora stata affrontata nel capitolo, il try e il catch vanno messi nel main o sbaglio? se potresti farmi un esempio sul codice che ho postato ti sarei grato .thanks
|
|
netarrow (Admin)
Guru^2
Messaggi: 2502
Iscritto: 12/05/2004
|
Codice sorgente - presumibilmente Java |
public class Propagate { public static void main (String args []) { try { reverse(r); ex.printStackTrace(); } } if(s. length() == 0 ) throw new Exception("Stringa vuota"); for (int i= s.length()-1; i>=0; --i){ reverseStr += s.charAt(i); } return reverseStr; } }
|
Qui ho usato una generica classe Exception, sarebbe da creare una tua eccezione che estende Exception tipo NullStringException.
|
|
Sventrip (Normal User)
Newbie
Messaggi: 19
Iscritto: 20/07/2008
|
Propagating and Catching an Exception
In this exercise you're going to create two methods that deal with exceptions. One of the methods is the main() method, which will call another method. If an exception is thrown in the other method, main() must deal with it. A finally statement will be included to indicate that the program has completed. The method that main() will call will be named reverse, and it will reverse the order of the characters in a String. If the String contains no characters, reverse will propagate an exception up to the main() method.
- Create a class called Propagate and a main() method, which will remain
empty for now.
- Create a method called reverse. It takes an argument of a String and returns
a String.
- In reverse, check if the String has a length of 0 by using the
String.length() method. If the length is 0, the reverse method will
throw an exception.
- Now include the code to reverse the order of the String.
- Now in the main() method you will attempt to call this method and deal with
any potential exceptions. Additionally, you will include a finally statement
that displays when main() has finished.
Questo esercizio viene proposto prima della dichiarazione ed estensione delle eccezioni, anche io avevo pensato a una soluzione come la tua ma perche sono andato avanti a leggere..avendo solo try catch e finally come si potrebbe fare?? e' questo il mio dilemma
|
|
netarrow (Admin)
Guru^2
Messaggi: 2502
Iscritto: 12/05/2004
|
L'alternativa sarebbe far scaturire l'eccezione in un altro modo, indiretto.
Ad esempio "bucando" la stringa accedendo alla 2 lettera quando invece c'è solo una, e questo genera una ArrayIndexOutOfBoundsException (o un'altra eccezione simile con nome diverso, non ricordo).
Ma sta di fatto che il testo dice di "controllare la dimensione e lanciare un'eccezione"
In reverse, check if the String has a length of 0 by using the
String.length() method. If the length is 0, the reverse method will
throw an exception.
|
questo può voler dire solo quello che abbiamo fatto.
Poi per il finally basta che lo aggiungi alla fine dopo il catch e con un println dici che è finito il programma.
Codice sorgente - presumibilmente Java |
... } catch(blablbla) { blablabla } finally { blablabla } ...
|
Ultima modifica effettuata da netarrow il 13/01/2010 alle 19:45 |
|
Sventrip (Normal User)
Newbie
Messaggi: 19
Iscritto: 20/07/2008
|
oook diciamo che e' risolto allora sbagliano anche nei libri questo e' da tenere conto...
|
|