Wednesday, 15 August 2012

java - Try-Catch-Finally - Final Block not recognising variable -



java - Try-Catch-Finally - Final Block not recognising variable -

firstly know should using try-catch resources, don't have date jdk on system.

i have next code below, , trying ensure resource reader closed using block, code below doesn't compile 2 reasons. firstly reader may have not been initialized , secondly close() should caught within own try-catch. dont both of these reasons defeat object of initial try-catch block?

i can solve issue block close() statement putting in own try-catch. still leaves compile error reader not beingness initialized?

i'm presuming have gone wrong somewhere? help appreciated!

cheers,

public path [] getpaths() { // create , initialise arraylist paths stored in when read // file. arraylist<path> pathlist = new arraylist(); bufferedreader reader; seek { // create new buffered read read lines file reader = files.newbufferedreader(importpathfile); string line = null; int = 0; // each line file, add together array list while((line = reader.readline()) != null) { pathlist.add(0, paths.get(line)); i++; } } catch(ioexception e) { system.out.println("exception: " + e.getmessage()); } { reader.close(); } // move contents arraylist path [] , homecoming function. path patharray [] = new path[(pathlist.size())]; for(int = 0; < pathlist.size(); i++) { patharray[i] = paths.get(pathlist.get(i).tostring()); } homecoming patharray; }

there no other way initialize buffer , grab exception. compiler right.

bufferedreader reader = null; seek { // stuff } catch(ioexception e) { // handle } { if(reader != null) { seek { reader.close(); } catch(ioexception e1) { // handle or forget } } }

the method close need try-catch-block since declares throw ioexception. doesn't matter if phone call in block or somewhere else. needs handled. checked exception.

read must initialized null. imho super useless, that's java. how works.

java try-catch-finally

No comments:

Post a Comment