Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Try catch confusion

 
Ravaa Bal
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// comments out everything to the end of the line, so your closing } is also commented out.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Ravaa Bal
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha! Thank you both, that explains a lot.
 
Muhammad Ali Khojaye
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ravaa Bal
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49773
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 488
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic