aspose file tools*
The moose likes Beginning Java and the fly likes Try catch finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Try catch finally" Watch "Try catch finally" New topic
Author

Try catch finally

Andreas Larsson
Greenhorn

Joined: Aug 22, 2012
Posts: 6
Why did i end up with two try?
This code works as intended.
The class Engine() throws Exception.
Any comments regarding how this code should look like is very appreciated.


Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Well, your catch and finally don't actually do anything, which raises the question, why did you add them at all? What are you trying to achieve?

It is almost always a horrible idea to have an empty catch block. At the very least, print or log an error message so that there's some record of what happened. Otherwise, something went wrong, but you have no idea what. Nor any way to find out.

Also, indentation should be consistent. The indentation of your braces { } seems to jump around randomly. I often don't even bother reading code if it isn't indented consistently.
Andreas Larsson
Greenhorn

Joined: Aug 22, 2012
Posts: 6
Thank you for the fast answer!

1: -Well, your catch and finally don't actually do anything, which raises the question, why did you add them at all? What are you trying to achieve?
-To get the code to compile, as i needed them due to the "throws Exception" and i belive starting a new thread can aswell throw an exception.
Edited: Seems i misread your statement, I thought you where forced to add a catch and finally if you implement try?

2: -It is almost always a horrible idea to have an empty catch block. At the very least, print or log an error message so that there's some record of what happened. Otherwise, something went wrong, but you have no idea what. Nor any way to find out.
-The program is very small and works without errors (its a finished work). I dont know enough about recording errors other then for debugging so at the moment so i leave them out.

3: -Also, indentation should be consistent. The indentation of your braces { } seems to jump around randomly. I often don't even bother reading code if it isn't indented consistently.
-I've looked around for a guide regarding indentation and standardization in general but failed to find any. As for this code i try and match the start and end curly bracers that contain code while leaving empty ones together.
On a side note, getting the indentation where you want it seems tricky even with the preview function.


It seems i need to be more specific in asking my questions, this was my first time
Divya Janyavula
Greenhorn

Joined: May 24, 2011
Posts: 19
We should write try/catch blocks only if we expect to handle exceptions. This is a link for tutorial on Exceptions http://docs.oracle.com/javase/tutorial/essential/exceptions/. This will help you in understanding when to write try/catch blocks and how you can use catch and finally blocks efficiently.

As far as indentation is concerned if you are using an editor like NetBeans or Eclipse you will have built-in support for formatting the contents.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Andreas Larsson wrote: . . . -To get the code to compile, as i needed them due to the "throws Exception" and i belive starting a new thread can aswell throw an exception.
Have you checked whether starting a Thread causes Exceptions?
It would appear your Engine constructor (not “class”); is incorrectly written if it declares it throws Exception and there is no need to handle it.
Andreas Larsson wrote:
. . .
-The program is very small and works without errors (its a finished work). . . .
And you never wear seat belts because you have never suffered a fatal accident. Either you have hidden the errors from yourself, or (as I said before) the declaration of Exception is itself an error.
Andreas Larsson wrote:
-I've looked around for a guide regarding indentation and standardization in general but failed to find any. . . .
Try here and here. Both links are quite old, and neither receives general and total acceptance.
Andreas Larsson
Greenhorn

Joined: Aug 22, 2012
Posts: 6
Divya Janyavula thank you for the link I immediately read it, it was very helpful. I use eclipse and it keeps indenting as I type the code however this doesn't feel enough to produce readable code.

Campbell Ritchie
-Have you checked whether starting a Thread causes Exceptions?
-I was up too late when typing this, I apologize.

-It would appear your Engine constructor (not "class"); is incorrectly written if it declares it throws Exception and there is no need to handle it.
-Indeed it is my constructor that throws Exception and not my class, thank you for the correction I will read up on the API regarding java.awt.Robot and java.awt.event.ActionListener to see why I needed "throws Exception".

-And you never wear seat belts because you have never suffered a fatal accident. Either you have hidden the errors from yourself, or (as I said before) the declaration of Exception is itself an error.
-I have suffered only minor accidents and so I became complacent, I've now learned that you can read the API to predict and understand error handling rather then waiting for them to happen.

-Try here and here. Both links are quite old, and neither receives general and total acceptance.
-This is incredible it's just what Im been looking for! Thank you soo much

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
You should be able to find out what happens about Threads and Exceptions from that link. Go through the link and it should tell you where Exceptions occur, and if you click on the name of the Exception, you can find whether RuntimeException appears in the inheritance hierarchy or not. The link Divya Janyavula gave you earlier will explain what that means.
You will find constructors which declare Exceptions, like this one; the writer thought it possible that an Exception might occur and the circumstances are given (very briefly) in the comments. (That is one of the better comments about Exceptions I have seen ‍). Did whoever wrote the Engine class think it was likely to throw an Exception? They ought to have told you why, in the documentation. It would have been better to throw a more specific Exception, eg
Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.
Mike S is quite right that you should never use an empty catch; what you are saying is the constructor says it throws an Exception but you don’t believe it. The risk with an empty catch is that you think the code is working correctly, when it isn’t, and become complacent. I used to help investigate road accidents and have come across several people who died because of not wearing a seat belt correctly. Your Engine class probably cannot harm anybody, but if you ever write code which controls traffic lights, cash machines, etc., make sure you are not complacent about its workings.

The nested try is a perfectly valid Java idiom. It would be more obvious if indented correctly, and there are some real‑life examples here … followed by a better version.
Andreas Larsson
Greenhorn

Joined: Aug 22, 2012
Posts: 6
Campbell Ritchie
-You will find constructors which declare Exceptions, like this one;
-Very nice link i will study this further.

-Did whoever wrote the Engine class think it was likely to throw an Exception?
-What can I say Eclipse told me do to it

-They ought to have told you why, in the documentation.
-I wish I told myself why would have been much eaiser in retrospect.

-Your Engine class probably cannot harm anybody
-Unless someone with an important job thinks its a good idea to let my program right click his mouse for him.

-The nested try is a perfectly valid Java idiom.
-I belived I had done something wrong ending up with two try's. Now I just need to review it
and the cause for this post is answered

-real-life examples here
-This will certainly help me understand it better.


Thank you all for the overwhelming amount of helpful replies to a poorly written post, not only did I recieve all my answers and beyond but I also gained a valuable insight on how my post was viewed.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
You’re welcome and sorry for being sarcastic about seat belts.

And always beware of anything which tells you to do something, even Eclipse. Lots of people say, “Campbell told me to …” in the hope their wives won’t get annoyed with them You may find that Eclipse thing was one of several suggestions.
Andreas Larsson
Greenhorn

Joined: Aug 22, 2012
Posts: 6
Campbell Ritchie
-sorry for being sarcastic about seat belts.
-No need to be sorry I am one of those people that allways wear my seatbelt correctly, atleast when the car is moving
I'm still at the stage where my eyes twinkles with joy when a program performs its intended purpose no matter what shortcut I took to get there.
Though after this experience I feel more motivated to remove bad habits as in the long run this will save me time and trouble.

-And always beware of anything which tells you to do something, even Eclipse.
-If I had properly investigated what Eclipse told me this post might never had cause to exist.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Andreas Larsson wrote: . . . -If I had properly investigated what Eclipse told me this post might never had cause to exist.
. . . and you would have learned so much less
Andreas Larsson
Greenhorn

Joined: Aug 22, 2012
Posts: 6
Campbell Ritchie
-. . . and you would have learned so much less
-Indeed, I would also have missed out on the motivation boost
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Try catch finally