This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Try catch confusion

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// comments out everything to the end of the line, so your closing } is also commented out.
 
Sheriff
Posts: 22511
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aha! Thank you both, that explains a lot.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74390
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic