This site requires JavaScript, please enable it in your browser!
Greenfoot back
FrozenForm
FrozenForm wrote ...

2012/6/3

A question about continuing after exception.

FrozenForm FrozenForm

2012/6/3

#
I am using a text file to save the high score of a game. If the file doesn't exist and the game is exported as a .jar app everything works fine, but when i export it as a web page, obviously the game can't create the score file. So my question is how do i get the program to continue after failing to create the file. This creates and reads the file: String hS; File scores = new File("scores.txt"); public void act() { if(!scores.exists()){ try{ scores.createNewFile(); } catch (Exception e){} //I'm guessing this is where it stops after failing file creation if(scores.exists()){ try{ BufferedWriter out = new BufferedWriter(new FileWriter("scores.txt")); out.write(""+0); out.close(); } catch(Exception e){} } } if(scores.exists()){ try{ BufferedReader in = new BufferedReader(new FileReader("scores.txt")); hS = in.readLine(); } catch(Exception e){} } else hS = "Not available..."; setText(""+hS); }
davmac davmac

2012/6/3

#
If you actually catch the exception, then execution continues at the catch point (so your comment above is not on the mark). In this case the exception occurs when you do 'scores.exists()', which isn't inside a try ... catch block, so the exception causes execution to terminate.
FrozenForm FrozenForm

2012/6/4

#
Ok, thank you, it's my first time doing something like this.
You need to login to post a reply.