FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   How do I not crash when loadStrings fails
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: How do I not crash when loadStrings fails  (Read 506 times)
st33d

WWW Email
How do I not crash when loadStrings fails
« on: Dec 30th, 2004, 11:26pm »

I have gotten so far in creating a web-crawler. However, unparsable files and hrefs that are javascript commands crash the spider on the spot.
 
Do I have to write a condition for every possible file type and href I may encounter, or can I stop loadStrings() crashing the applet with some sort of verification as to whether loadStrings() can reach a file or not? The latter would be preferable as it would save a lot of bother and future proof the code. Especially seeing as the spider currently surfs at random.
 
Does anyone know of a workaround?
 

I could murder a pint.
fjen

WWW
Re: How do I not crash when loadStrings fails
« Reply #1 on: Dec 31st, 2004, 2:41am »

have you tried a try-catch approach?
 
Code:

try {
loadStrings("somefile.txt");
} catch (Exception e) { e.printStackTrace(); }

 
/F
 
st33d

WWW Email
Re: How do I not crash when loadStrings fails
« Reply #2 on: Dec 31st, 2004, 4:04am »

I've only seen try/catch in java code I've browsed through so I may have this wrong but trying to run the following code doesn't work.
 
Code:

try {
rect(width-10,height-10,10,10);
loadStrings("somefile.txt");
} catch (Exception e) {
rect (0,0,10,10);
e.printStackTrace();
}

 
I even tried some alternative exceptions in the link below but as I understand it, Exception should catch it.
 
http://mindprod.com/jgloss/exception.html
 
I'm running v.68 if that makes a difference. I tried exporting the applet but I still get the same old frozen applet effect.
 
The effect of looking for a file that isn't there is exactly the error I'm trying to deal with. I tried IOException and FileNotFoundException but then it insists that the catch block is unreachable. Any suggestions?
 

I could murder a pint.
toxi

WWW
Re: How do I not crash when loadStrings fails
« Reply #3 on: Dec 31st, 2004, 11:08am »

the "problem" (or feature) is that loadStrings("some.txt") already has its own internal "try-catch" block in place, so the exception never reaches your code. that approach keeps the problem away and doesn't crash your sketch, if you know how to interpret the result of loadStings():
 
Code:
String[] lines=loadStrings("some.txt");
 
// when successful "lines" is a valid array of strings
// if loading failed, lines=null
if (lines!=null) {
  println(lines.length+" lines loaded");
} else {
  println("loading failed.");
}
println("code continues as usual");

 

http://toxi.co.uk/
st33d

WWW Email
Re: How do I not crash when loadStrings fails
« Reply #4 on: Dec 31st, 2004, 1:21pm »

Thanks, that's magic. I was also going to ask about trouble with "out of bounds" exceptions because "Exception" doesn't seem to seem to catch those but I've found that "Throwable" does.
 
Code:

String [] test = new String[1];
try {
println(test[1]);
} catch (Throwable e) {
println("array out of bounds");
}

 
Oh wait. This works too.
 
Code:

String [] test = new String[1];
try {
println(test[1]);
} catch (IndexOutOfBoundsException e) {
println("array out of bounds");
}

 
Thanks again. I'll sit down and rework the spider into something tidier now.
 

I could murder a pint.
fjen

WWW
Re: How do I not crash when loadStrings fails
« Reply #5 on: Dec 31st, 2004, 6:10pm »

ok. just remember to use this only as emergency exit. clean, working code is always first choice ..
 
/F
 
Pages: 1 

« Previous topic | Next topic »