ich hab mal eine Frage zu Exceptions. ich habe folgende Methode:
Code:
public int getKeywordPosition( String kw ) throws JDOMException {
// get a list of all keywords of the keywordfile
List<?> keywordList = XPath.selectNodes( keywordFile, "/keywords/entry/" );
// counter for the return value if a found keyword matches the parameter
int cnt = 1;
// iterate all elements of the keyword list
for ( Object object : keywordList )
{
Attribute attribute = (Attribute) object;
// if keyword matches the parameter string, return the position
if( kw.equals(attribute.getValue() )) return cnt;
// else increase counter
cnt++;
}
// if no keyword was found, return -1
return -1;
}
keywordFile ist ein XML-Dokument vom Typ Document.
Meine Fragen: Wann wird hier möglicherweise eine JDOMException verursacht? Und: Kann ich diese dann mit try/catch abfangen, sodass ich vorher keine if-Abfragen machen muss, die bspw. überprüfen, ob auch tatsächlich Elemente im keywordFile enthalten sind?
Vielleicht noch ein kurzes anderes Beispiel:
Code:
public String getKeyword( int pos ) throws JDOMException {
// create a path to the selected entry
String path = "/keywords/entry["+String.valueOf(pos)+"]";
// get the single element which matches the given position (path)
Object kw = XPath.selectSingleNode( keywordFile, path);
// return the value of the element, i.e. the requested keyword
return ((Attribute) kw).getValue();
}
Wenn der Parameter pos nun auf ein Element zeigt, das es gar nicht gibt (z.B. es gibt 5 Elemente, pos enthält aber den Wert 7), wird dann eine JDOMException verursacht? Und könnte ich dann im "catch"-Bereich das angeben, was passieren soll, wenn der Parameter nicht zulässig ist? (also bspw. einen leeren String zurückgeben)
Meine Frage bezog sich eher allgemein auf das Prinzip von Exceptions.
NetBeans bietet ja an, automatisch einen try/catch-Block um eine Auswahl zu setzen. Meine untere Funktion aus dem obigen Beispiel sieht jetzt so aus:
Code:
public String getKeyword( int pos ) throws JDOMException {
// create a path to the selected entry
String path = "/keywords/entry["+String.valueOf(pos)+"]";
try {
// get the single element which matches the given position (path)
Object kw = XPath.selectSingleNode(keywordFile, path);
// return the value of the element, i.e. the requested keyword
return ((Attribute) kw).getValue();
}
catch (JDOMException jDOMException) {
// if element doesn't exist due to invalid parameter, return an empty string
return("");
}
}
Allerdings kommt bei der oberen der Hinweis, dass keine JDOMException, sondern nur eine Exception auftreten kann:
Code:
public int getKeywordPosition( String kw ) throws JDOMException {
// get a list of all keywords of the keywordfile
List<?> keywordList = XPath.selectNodes( keywordFile, "/keywords/entry/" );
// counter for the return value if a found keyword matches the parameter
int cnt = 1;
try {
// iterate all elements of the keyword list
for (Object object : keywordList) {
Attribute attribute = (Attribute) object;
// if keyword matches the parameter string, return the position
if (kw.equals(attribute.getValue())) {
return cnt;
// else increase counter
}
cnt++;
}
}
catch (Exception e) {
// if no keyword was found, return -1
return -1;
}
// if no keyword was found, return -1
return -1;
}
Ich bin da etwas unsicher, ob ich so den Umgang mit Exceptions richtig verstanden habe?
Ah, ich glaube, ich verstehe deinen Hinweis: Der try/catch-Block muss um diese Anweisung herum:
Code:
// get a list of all keywords of the keywordfile
List<?> keywordList = XPath.selectNodes( keywordFile, "/keywords/entry/" );
Sollte die Liste (d.h. die XML-Datei bzw. das Document) also leer sein, gibt es eine JDOMException, andernfalls gibt es mind. ein Element/Objekt in der Liste.
Die korrekte Verwendung wäre dann:
Code:
List<?> keywordList = null;
try {
// get a list of all keywords of the keywordfile
keywordList = XPath.selectNodes(keywordFile, "/keywords/entry/");
} catch (JDOMException e) {
return -1;
}
>> Ich bin da etwas unsicher, ob ich so den Umgang mit Exceptions richtig verstanden habe?
Wage mal zu behaupten, dass du es nicht verstanden hast
Exception selbst zu fangen ist in 99% der Fälle schlicht falsch, JDOMException zu fangen wäre richtig.
return -1 zurückzugeben ist etwas das man in C gemacht hat, obwohl es neuerdings wieder Strömungen gibt welche Returncodes gegenüber Exceptions bevorzugen, unterstelle ich dir schlicht dass du es wie in C machen möchtest
Lese dich mal in Exception Handling ein, dieses Wissen brauchst du immer in Java.
Da könntest du recht haben. In alter C-Gewohnheit frage ich vorher immer mit if-Abfragen ab, ob ein möglicher Fehler vorliegt, ob etwas geklappt hat etc.
Aber zum Rückgabewert -1: Ich rufe die Funktion ja von anderer Stelle auf, und dort brauche ich ja weiterhin die Information, ob ein Element existiert oder nicht. Daher habe ich einen Rückgabewert von -1 mit dabei. Wie sonst könnte ich an anderer Stelle feststellen, ob alles ok ist?
Bsp eines Aufrufs:
Code:
int kpos = myClass.getKeywordPosition( "Schlagwort" );
So, nun enthält kpos entweder einen Wert > 0, wenn das "Schlagwort" existiert, oder eben -1, falls nicht. Kann ich das wiederum anders abfragen als mit einer if-Abfrage, sodass ich den Rückgabewert von -1 nicht mehr benötige?