Exceptions

tanzverfuehrung

Bekanntes Mitglied
meine methode getnode sagt aus, das es eine IllegalArgument Exception gibt ,wenn der Pfad name ungültig ist!
aber wie kann ich das richtig einbinden in meine methode???


Java:
@Override
	public boolean nodeExists(String pathName) throws IllegalArgumentException {

		try{
		if(isValidPath(pathName) == true)
			System.out.println("Wir haben einen gültigen PfadName!");
		}catch (IllegalArgumentException e) {
			System.out.println(e.getMessage());
		}
 
				
		if (pathName.isEmpty()) {
			return false;
		}

		int index = pathName.indexOf("/");

		if (index == 0) { // absoluter Pfad

			// zum relativen pfad machen zur weiter verarbeitung
			pathName = pathName.substring(index + 1);
		}

		SimplePreferences value = nodeMap.get(pathName);
		if (value != null)
			return true;

		index = pathName.indexOf("/");

		if (index < 0) { // Wenn der index < 0 ist wurde kein Slash mehr
							// gefunden, dann ist es der letzte Knoten
			value = nodeMap.get(pathName); // prüfen ob knoten
			// vorhanden ist
			if (value != null)
				return true;
			return false;
		}
		String[] split = pathName.split("/");
		for (int i = 0; i < split.length; i++) {
			value = nodeMap.get(split[i]); // prüfen ob knoten
			// vorhanden ist
			if (value != null)
				return true;
		}
		return false;
	}
	
	
	public boolean isValidPath(String pathnName){
		
		if(pathnName.endsWith("/")){
			return false;
		}
		int index =pathnName.indexOf("//");
		if(index > -1){
			return false;
		}
		return true;		
	}
 
S

Sym

Gast
Ich verstehe leider nicht, was Du meinst? Welche getNode-Methode? Was willst Du wo einbinden?
 

tanzverfuehrung

Bekanntes Mitglied
Ich verstehe leider nicht, was Du meinst? Welche getNode-Methode? Was willst Du wo einbinden?

sorry ich meinte in meiner nodeExists methode
nodeExists
public abstract boolean nodeExists(String pathName)
throws BackingStoreExceptionReturns true if the named preference node exists in the same tree as this node. Relative path names (which do not begin with the slash character ('/')) are interpreted relative to this preference node.
If this node (or an ancestor) has already been removed with the removeNode() method, it is legal to invoke this method, but only with the path name ""; the invocation will return false. Thus, the idiom p.nodeExists("") may be used to test whether p has been removed.


Parameters:
pathName - the path name of the node whose existence is to be checked.
Returns:
true if the specified node exists.
Throws:
BackingStoreException - if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
IllegalArgumentException - if the path name is invalid (i.e., it contains multiple consecutive slash characters, or ends with a slash character and is more than one character long). NullPointerException - if path name is null. s * @throws IllegalStateException if this node (or an ancestor) has been removed with the removeNode() method and pathName is not the empty string ("").

--------------------------------------------------------------------------------


dazu habe ich jetzt ne mthode geschrieben(siehe code) isValidPath um zu ermitteln ob es ein gültiger pfad name ist!ich habe jetzt aber keine ehnung wie ich die exception in meine MEthode nodeExists kriege!
so das meine junit tests grün werden

Junittests
Java:
	@Test(expected=IllegalArgumentException.class)
	public void testNodeExistsWithSlashAtTheEnd() throws Exception {
		simple.nodeExists("bc/");
	}
	@Test(expected=IllegalArgumentException.class)
	public void testNodeExistsWithInvalidPath() throws Exception {
		simple.nodeExists("b//c");
	}
 

André Uhres

Top Contributor
Java:
public boolean nodeExists(final String pathName) throws IllegalArgumentException {
    if (!isValidPath(pathName)) {
        throw new IllegalArgumentException("Der Pfadname ist ungültig: '" + pathName + "'");
    }
 
S

Sym

Gast
Java:
throw new IlligalArgumentException()
Damit wirfst Du in Deiner isValidPath-Methode eine Exception, die in Deiner nodeExists-Methode behandelt wird.
 
S

Sym

Gast
Java:
public boolean nodeExists(final String pathName) throws IllegalArgumentException {
    if (!isValidPath(pathName)) {
        throw new IllegalArgumentException("Der Pfadname ist ungültig: '" + pathName + "'");
    }
Ich habe ihn so verstanden, dass er die Exception in der isValidPath-Methode werfen möchte. Immerhin behandelt er diese schon in der nodeExists-Methode.
 

tanzverfuehrung

Bekanntes Mitglied
Und wieder ein weiterer glücklicher Mensch... :D

ohhh ja das kann man so sagen!:oops::toll::D

aber habe noch ne kleine frage,
wollte jetzt noch für die methode isValidPAth junit test schreiben
aber es geht nicht!
er sagt immer
The method isValidPath(String) is undefined for the type SimplePreferencesTest

also ich kann die methode noicht richtig aufrufen!?
WHY????:L
ja undefiniert sagt er....aber :bahnhof:???:L
Java:
	@Test
	public void testIsValidPath() throws Exception {
	assertTrue(isValidPath("a/b/c"));
	}
	@Test
	public void testUnValidPath() throws Exception {
	assertFalse(isValidPath("a//b/c"));
	}
	@Test
	public void testUnValidPathSlashAtTheEnd() throws Exception {
	assertFalse(isValidPath("a//b/c/"));
	}
 
S

Sym

Gast
Du möchtest hier doch die Methoden einer anderen Klasse testen.

Also versuch es mal so:
Java:
@Test
public void testIsValidPath() throws Exception {
    assertTrue(new SimplePreferences().isValidPath("a/b/c"));
}

(Wenn Deine Klasse SimplePreferences ist)
 

tanzverfuehrung

Bekanntes Mitglied
Du möchtest hier doch die Methoden einer anderen Klasse testen.

Also versuch es mal so:
Java:
@Test
public void testIsValidPath() throws Exception {
    assertTrue(new SimplePreferences().isValidPath("a/b/c"));
}

(Wenn Deine Klasse SimplePreferences ist)

so glücklicher kann ich heut nciht mehr werden
DANKE:toll::applaus:
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
M Test auf Exceptions schreiben Java Basics - Anfänger-Themen 11
berserkerdq2 Habe zwei exceptions, welche ist ein Kommunikationsfehler und welche ein Ausgabefehler? Java Basics - Anfänger-Themen 4
julian112 Input/Output .gz bzw. .txt Datei Einlesen und Umgang mit Exceptions Java Basics - Anfänger-Themen 1
C Exceptions identifizieren Java Basics - Anfänger-Themen 5
A Exceptions mit objektreferenzen Java Basics - Anfänger-Themen 4
A Exceptions und methods Java Basics - Anfänger-Themen 2
A Cannot find symbol bei exceptions Java Basics - Anfänger-Themen 2
A Exceptions und Packages Java Basics - Anfänger-Themen 6
B JUnit / Exceptions/ try-catch Java Basics - Anfänger-Themen 6
X Exceptions Benutzereingaben Java Basics - Anfänger-Themen 4
F Exceptions in Interfaces Java Basics - Anfänger-Themen 4
F Mehrere Exceptions in einem Catch-Block abfangen Java Basics - Anfänger-Themen 12
L Exceptions und Konten Java Basics - Anfänger-Themen 21
D Frage zu Exceptions Java Basics - Anfänger-Themen 8
I Wie programmiert man Exceptions? Java Basics - Anfänger-Themen 4
N Unterschied zwischen Checked und Unchecked Exceptions Java Basics - Anfänger-Themen 12
C Erste Schritte Exceptions nicht verstanden Java Basics - Anfänger-Themen 2
J Fragen zu Exceptions Java Basics - Anfänger-Themen 24
T Exceptions - ausgeführte Zeilen Java Basics - Anfänger-Themen 4
J Exceptions Java Basics - Anfänger-Themen 69
C Exceptions Java Basics - Anfänger-Themen 8
C Exceptions Java Basics - Anfänger-Themen 6
A ArrayQueue mit Exceptions und Vererbung Java Basics - Anfänger-Themen 3
F Exceptions Java Basics - Anfänger-Themen 6
J Frage zum Thema Exceptions (Try/Catch) Java Basics - Anfänger-Themen 3
M "Exceptions abfragen" Java Basics - Anfänger-Themen 6
Farbenfroh Exceptions Anfänger - Finde Fehler nicht Java Basics - Anfänger-Themen 7
Z Catch & Exceptions Java Basics - Anfänger-Themen 4
N Compiler-Fehler Drei Exceptions in GUIHack für Dreiecke auf MoveButtons Java Basics - Anfänger-Themen 36
V Welche Exceptions müssen importiert werden? Java Basics - Anfänger-Themen 3
S Exceptions Java Basics - Anfänger-Themen 7
M Vererbung Problem Vererbung/Exceptions Java Basics - Anfänger-Themen 9
S Verschachtelte Exceptions - Übersicht verbessern Java Basics - Anfänger-Themen 2
J Eclipse Exceptions Java Basics - Anfänger-Themen 2
K Schleifen und Exceptions Java Basics - Anfänger-Themen 8
K Exceptions auslagern Java Basics - Anfänger-Themen 15
R NullPointer Exceptions Java Basics - Anfänger-Themen 3
F Erste Schritte Übung zu Exceptions Java Basics - Anfänger-Themen 24
R Exceptions (try/catch) Java Basics - Anfänger-Themen 63
H Int Exceptions Java Basics - Anfänger-Themen 12
M Exceptions per throws oder try Java Basics - Anfänger-Themen 4
M Compiler-Fehler Queue als ArrayList mit Exceptions Java Basics - Anfänger-Themen 3
T Exceptions in einer Klasse Java Basics - Anfänger-Themen 3
B Eigene Exceptions entwerfen Java Basics - Anfänger-Themen 3
H Methoden Überflüssige Exceptions Java Basics - Anfänger-Themen 20
C Exceptions Java Basics - Anfänger-Themen 14
1 While Schleife Exceptions Java Basics - Anfänger-Themen 6
I Erste Schritte Eigene Fehlermeldungen bei Exceptions Java Basics - Anfänger-Themen 19
D Frage zu Exceptions Java Basics - Anfänger-Themen 12
M Compiler-Fehler Exceptions lieber throwen oder direkt catchen? Java Basics - Anfänger-Themen 8
T Exceptions Java Basics - Anfänger-Themen 19
B Wie finde ich Exceptions? Java Basics - Anfänger-Themen 19
Dit_ Input/Output Alle Exceptions protokollieren Java Basics - Anfänger-Themen 9
J Standard Exceptions abfangen Java Basics - Anfänger-Themen 5
F Exceptions werfen oder catchen?? Java Basics - Anfänger-Themen 14
D Exceptions - Ausnahmebehandlung Java Basics - Anfänger-Themen 19
D Frage zu Exceptions und der import Anweisung Java Basics - Anfänger-Themen 12
J Paar Fragen zu Exceptions Java Basics - Anfänger-Themen 16
G Verständnisproblem: Exceptions Java Basics - Anfänger-Themen 17
S Exceptions bei push/pop in Stack Java Basics - Anfänger-Themen 8
C Exceptions beim Beenden Java Basics - Anfänger-Themen 2
C TimerTask und Exceptions Java Basics - Anfänger-Themen 5
E Klasse öffnen, mehrere Exceptions Java Basics - Anfänger-Themen 9
C Exceptions Java Basics - Anfänger-Themen 7
G 2 Exceptions in einer Methode Java Basics - Anfänger-Themen 3
firefexx Exceptions werfen Java Basics - Anfänger-Themen 5
0 Exceptions mehrfach fangbar? Java Basics - Anfänger-Themen 4
O Exceptions Java Basics - Anfänger-Themen 3
K Sinn eigener Exceptions Java Basics - Anfänger-Themen 11
H Diverse Exceptions - Troubleshooting Java Basics - Anfänger-Themen 3
J exceptions Java Basics - Anfänger-Themen 8
sc0p InterruptedExceptions und Exceptions - in Einem! Java Basics - Anfänger-Themen 5
M Frage zu Exceptions Java Basics - Anfänger-Themen 19
M Fragen zu Exceptions Java Basics - Anfänger-Themen 3
A Exception Verständnisfrage: Exceptions während, einer Statischenzuweisung abfangen Java Basics - Anfänger-Themen 10
D Exceptions werfen + beenden Java Basics - Anfänger-Themen 12
M Exceptions aus interface-Methoden Java Basics - Anfänger-Themen 2
S File.renameTo und Exceptions / Fehlermeldung Java Basics - Anfänger-Themen 2
B Exceptions in Liste sammeln? Java Basics - Anfänger-Themen 5
O Eigene Exceptions Java Basics - Anfänger-Themen 11
O "restliche" Exceptions fangen Java Basics - Anfänger-Themen 8
H [Stil] Exceptions in der Klasse behandeln oder throwen? Java Basics - Anfänger-Themen 62
T Problem beim Werfen und Fangen von Exceptions Java Basics - Anfänger-Themen 2
V Aktivitätsdiagramm / Exceptions Java Basics - Anfänger-Themen 5
V Exceptions Java Basics - Anfänger-Themen 6
K Frage zu Exceptions -> Logging Java Basics - Anfänger-Themen 6
M Eigene Fehlermeldung bei Exceptions? Java Basics - Anfänger-Themen 12
R JDom Exceptions Java Basics - Anfänger-Themen 4
R Datei einlesen mit Exceptions Java Basics - Anfänger-Themen 2
Daniel_L Verwendung von try und catch bei exceptions Java Basics - Anfänger-Themen 7
C Reflection Exceptions behandeln Java Basics - Anfänger-Themen 6
G Exceptions - spiegeln wir da nicht einen Spiegel im Spiegel? Java Basics - Anfänger-Themen 10
G Verschiedene Exceptions zu gleichem Block Java Basics - Anfänger-Themen 6
U Frage zu Exceptions Java Basics - Anfänger-Themen 5
mwildam Philosophiefrage zu Exceptions und Rückgabewerten Java Basics - Anfänger-Themen 6
D Static, final Objekte mit Exceptions im Konstruktor Java Basics - Anfänger-Themen 2
G Exceptions Java Basics - Anfänger-Themen 4
G ServerSocket: Exceptions und Timeout Probleme Java Basics - Anfänger-Themen 10
M Exceptions bei Textfeldern abfangen Java Basics - Anfänger-Themen 2
P Problem mit exceptions Java Basics - Anfänger-Themen 9

Ähnliche Java Themen

Neue Themen


Oben