Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Help in java!

  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Help in java!



    Hi guys, i need help understanding interface and exceptions. I really do not understand the idea behind it. Can someone explain using a example? It will be really nice!
    Thanks.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Interfaces and exceptions are completely distinct in what they need to do. There is no direct relationship between the two as there is between a class and and interface (logically that is, Exceptions can certainly implement Interfaces).
    Interfaces are simply a contract that guarantees typeof Interface will have x method available to use. Any class implementing an interface will inherit its datatype as well and this is the only way to perform multiple inheritance in Java (and also why interfaces are 1000x more useful than a base extends).
    Exceptions are thrown when the program doesn't know what to do with data provided or how to deal with bad returns. Its easiest thought of as a 'pass the buck' type idea - I don't know what to do with this, so you figure it out. The File will throw a FileNotFoundException if the path is bad. But it doesn't know what you want to do with it, so you try/catch it when calling file, and may ask for input again on a file. Maybe its a configuration file instead, so you print an error and terminate the application.
    Exceptions can be checked or unchecked. Checked exceptions force the caller to try/catch or further send the caller by marking the method as checked and adding the throws XException to it, effectively making the caller of that method responsible for dealing with it. This can be done indefinitely, but really should be try/caught no higher than main. Above main, the VM will instead catch the exception and toss a fatal termination of the program.
    Unchecked do not require a preemptive try/catch. Without one though, if the exception is tossed it will terminate the program.

    Also, in the future please select a title more suiting to the question you are asking or problem you are having.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •