Results 1 to 2 of 2
  1. #1
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default throws Exception

    Hi,

    While declaring objects of some classes, like FileInputStream, FileOutputStream, ObjectInputStream etc, we need to declare them in a try catch block. Otherwise we get exception saying Unhandled exception. One other way is to use throws exception with the method name in which we are declaring the object. For example:

    Java Code:
    public void myFunction() throws Exception{
    FileInputStream f_in = new FileInputStream("C:\\myvector.data");
    ...
    }
    Which is the right way?

    Thanks in advance.

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Both are right. However, you will have to use a try/catch block eventually. If you tack on a 'throws Exceptioin' to your method signature, you will have to put all calls to that method in a try/catch block.

Similar Threads

  1. Where does the exception go?
    By aytidaalkuhs in forum New To Java
    Replies: 3
    Last Post: 04-07-2008, 02:24 PM
  2. Replies: 1
    Last Post: 03-28-2008, 05:08 PM
  3. Replies: 1
    Last Post: 03-08-2008, 06:41 PM
  4. Difference between Throws and Throw
    By Poonam in forum New To Java
    Replies: 7
    Last Post: 02-06-2008, 04:52 PM
  5. Main method with throws Exception
    By bugger in forum New To Java
    Replies: 3
    Last Post: 01-07-2008, 02:48 PM

Posting Permissions

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