• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dead code in if block?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
the following code shows Dead code in Eclipse from line 20 to 38 ( i tried to underline but this doesnt work in code)

does eclipse assume that the variable fis can never have the value null ?
i have this problem at a lot of places always the same style of code so solving it would save me around 50 warnings!

please comment if you understand why this happens ;)

cheers
Dirk


 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the assignment on Line 16 is successful, then the condition on Line 19 will never, ever evaluate to true, therefore, there is no chance for the code you wanted to underline to execute. BTW, you can't put any other formatting tags inside [ code] tags.
 
dirk dj jaeckel
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:If the assignment on Line 16 is successful, then the condition on Line 19 will never, ever evaluate to true, therefore, there is no chance for the code you wanted to underline to execute. [ code] tags.



yes, but i think the code is there for the case the assignment on Line 16 is NOT succesfull ???

 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only reason a new won;t be succesful is if there is an exception inside constructor (or a fatal exception in JVM). In both cases, control will never go to line 19.

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jayesh is right, see the JavaDocs for java.io.FileInputStream - the constructor will throw a FileNotFoundException. The Java compiler sees that and figures out that's the only reason for execution to skip Line 19 and go to the catch block. Your check for null on Line 19 is redundant. In general, checks for null should be reconsidered. It makes your code unnecessarily noisy with all the if (something == null). You should write tests and establish a design convention where nothing should return null if null is not a valid value. For example, methods that are declared to return a collection such as a List should be designed to return an empty list if nothing is there to populate the list, it shouldn't return a null value. That way, client code doesn't have to check for null all the time. If the class does in fact return null when asked for a List, then that becomes a bug in the class implementation.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jayesh A Lalwani wrote:The only reason a new won;t be succesful is if there is an exception inside constructor (or a fatal exception in JVM). In both cases, control will never go to line 19.


There is also code called before the constructor is invoked - in this case, it's the "Settings.getInstance().getImagePath("moneyBackside")". But the same argument applies - if it throws an exception, control will never go to line 19. If it doesn't throw an exception, then line 19 does execute, and fis definitely has a non-null value.
 
Jayesh A Lalwani
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, it looks like the code in your catch block is doing the same things that your code in try block is executing. That's quite a bunch of redundant code. If you have an exception in main code, your code in catch block will fail too.

Do not use Exception to handle cases that can be handled using validation. For example, if your case, you should first check if the FIle exists. If file exists, open the file in FileInputStream. If it doesn't exist, get the InputStream from the Classloader. The code that loads the Image from InputStream and draws the image doesn't have to be duplicated.
 
dirk dj jaeckel
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jayesh A Lalwani wrote:Also, it looks like the code in your catch block is doing the same things that your code in try block is executing. That's quite a bunch of redundant code. If you have an exception in main code, your code in catch block will fail too.

Do not use Exception to handle cases that can be handled using validation. For example, if your case, you should first check if the FIle exists. If file exists, open the file in FileInputStream. If it doesn't exist, get the InputStream from the Classloader. The code that loads the Image from InputStream and draws the image doesn't have to be duplicated.



thanks for your comments

that is not my code and i am new in java, thats why i did this post, i´ll go over it and post my result then here.

 
dirk dj jaeckel
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what i made of it, please comment ;)

as far as i could test it it works pretty fine:


 
Jayesh A Lalwani
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are still using exception handling where you should be using validation.

You can do this

 
dirk dj jaeckel
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your code looks cleaner but doesn´t make anymore what was intended,
the try catch blocks are for getting the diffrence of application or applet
and when both have no internal resource then a load from a fixed path is tried,
thats what the comment of the original programmer says which is unfortunatly in german



Jayesh A Lalwani wrote:You are still using exception handling where you should be using validation.

You can do this

 
reply
    Bookmark Topic Watch Topic
  • New Topic