| Author |
Try catch confusion
|
Ravaa Bal
Ranch Hand
Joined: Apr 15, 2009
Posts: 31
|
|
This bit code is giving me the error "';' expected", and the statement inside the try catch block doesn't recognise that I'm trying to catch the exception that it wants caught. Not sure what I could be doing wrong...here's the full class in case that gives more of an idea:
Thank you in advance!
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
|
// comments out everything to the end of the line, so your closing } is also commented out.
|
Joanne
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
Ravaa Bal wrote:
That's a statement directly in a class, and that's not allowed. A class can only contain declarations - fields, methods, inner classes, etc - and blocks. So you can fix this as follows:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Ravaa Bal
Ranch Hand
Joined: Apr 15, 2009
Posts: 31
|
|
|
Aha! Thank you both, that explains a lot.
|
 |
Muhammad Ali Khojaye
Greenhorn
Joined: Oct 03, 2008
Posts: 1
|
|
Ravaa Bal wrote:
This bit code is giving me the error "';' expected", and the statement inside the try catch block doesn't recognise that I'm trying to catch the exception that it wants caught. Not sure what I could be doing wrong...here's the full class in case that gives more of an idea:
Better if you use the same code format style.
Style 1
try
{
//code
}
catch( MidiUnavailableException e)
{
}
Style 2
try {
// your code
} catch( MidiUnavailableException e) {
}
Will make your code more readable with consistent formatting style.
Hope it helps.
|
 |
Brian Legg
Ranch Hand
Joined: Nov 07, 2008
Posts: 488
|
|
I think everyone else answered your question. 1 other thing though, when using any resources that pull/send data from a file or stream you open a connection. You should always include a finally {} block after your catch to close these connections otherwise your program could go into a never ending state of waiting for the stream to end. Your code could work 99% of the time, but the one time it doesn't get the connection and it catches the exception unless you have a finally block your program will be stuck in a never ending state.
HTH
|
SCJA
~Currently preparing for SCJP6
|
 |
Ravaa Bal
Ranch Hand
Joined: Apr 15, 2009
Posts: 31
|
|
Thanks Muhammad for that tip, I shall try and put it to good use!
Brian Legg wrote:I think everyone else answered your question. 1 other thing though, when using any resources that pull/send data from a file or stream you open a connection. You should always include a finally {} block after your catch to close these connections otherwise your program could go into a never ending state of waiting for the stream to end. Your code could work 99% of the time, but the one time it doesn't get the connection and it catches the exception unless you have a finally block your program will be stuck in a never ending state.
HTH
Thanks Brian, do you mean after the last catch block I include another {} block with close() methods? I don't quite understand.
|
 |
Brian Legg
Ranch Hand
Joined: Nov 07, 2008
Posts: 488
|
|
Anytime you open a file or stream (etc) you put it in a try catch block, after any place you do this you should include a finally block to close the resource. If you are doing this in multiple places then you will need multiple finally clauses. I always include all the code that uses the resource inside the try block that way when I am done using the resource it will be closed for sure even if an exception is thrown. For instance...
try {
FileInputStream stream = new FileInputStream("myfile.txt");
...all code that uses stream goes here...
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} finally {
stream.close()
}
Hope that helps.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
Don't you need the finally inside a try, and don't you need an if (myStream != null) test before closing it?
The 1st is because closing might throw an Exception.The 2nd is because any Exceptions while opening the Stream might present a null Stream to be closed.
I wrote about that last week here.
|
 |
Brian Legg
Ranch Hand
Joined: Nov 07, 2008
Posts: 488
|
|
Good point Campbell... I should have learned my lesson about coding straight into the browser without an IDE
I don't have Java or a compiler at work so I type out some mistakes sometimes, but I think the key points are captured
|
 |
 |
|
|
subject: Try catch confusion
|
|
|