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

static method and exception

 
arjun rampal
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have file which is reading from file and deleting it.
delete is done by static method.
now i need to do like this if file is empty it should throw exception and delte the file.
so i added else like this
else{

throw new empty("Empty");
}
but it is only thorowing exception not deleting file.as that static code is unreachable after throwing exception.
any idea
 
John Kelty
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand right, you want to delete the file whether or not you throw an exception. Try placing the file delete logic in a finally {} block.

--John
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, find out if the file is empty and save that into a boolean variable. Then delete the file, then take any action based on the boolean that you think is appopriate.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic