Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: caught or declare might be thrown error

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default caught or declare might be thrown error

    public class CreateFileMain{
     
      public static void main(String[] args){
     
      CreateFile object = new CreateFile();
     
      object.openFile();
     
      object.writeFile();
     
      object.closeFile();
     
    }
    }

    import java.io.*;
     
    public class CreateFile{
     
      BufferedWriter in;
     
      public void openFile(){
     
        try{
     
        in = new BufferedWriter(new FileWriter("C:\\Users\\Hiep\\Desktop\\Java Programming\\file.text"));  
     
        System.out.println("shell, something is wrong");
     
         }catch(Exception e){
     
        }
      }
     
      public void writeFile(){
     
        in.write("You did it");
      }
     
      public void closeFile(){
     
        in.close();
     
      }
    }

    any idea. I did some research but i still can't fix it.
    Last edited by IDK12; January 28th, 2011 at 11:56 AM.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: caught or declare might be thrown error

        public void openFile() {
            try {
                in = new BufferedWriter(new FileWriter("C:\\Users\\Hiep\\Desktop\\Java Programming\\file.text"));
                System.out.println("shell, something is wrong");
            } catch (IOException e) {
            }
        }
     
        public void writeFile() {
            try {
                in.write("You did it");
            } catch (IOException ex) {
            }
        }
     
        public void closeFile() {
            try {
                in.close();
            } catch (IOException ex) {
            }
     
        }

    As all your methods do some sort of file handling, and the exceptions that could arise are checked exceptions, you're required to attempt to catch or throw a possible exception for all your methods.
    Unless say openFile() called writeFile(), then you would only require one try...catch block to handle both.

    On a side note, next time please try and structure your questions better, include more information about your problem and what you have tried yourself. The clearer the question the more help you will get.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Jul 2009
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: caught or declare might be thrown error

    import java.io.*;
     
    import java.lang.*;
     
    import java.util.*;
     
    public class CreateFile{
     
      private Formatter in;
     
      public void openFile(){
     
        try{
     
        in = new Formatter("C:\\Users\\Luke\\Desktop\\Java Programming\\file.text");
     
     
     
     
        System.out.println("shell, something is wrong");
     
         }catch(Exception e){
     
        }
      }
     
      public void writeFile(){
     
        in.format("%s%s%s","test","test","test");
      }
     
      public void closeFile(){
     
        in.close();
     
      }
    }

    can you tell me why this does require the IOException in every method? also, i don't understand why in need this IOexception
    Last edited by IDK12; January 28th, 2011 at 01:33 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: caught or declare might be thrown error

    You must catch checked Exceptions that may be thrown - it is a way of error handling in the code should something fail along the way. The API's that you use will specify exceptions - if any - that a certain method/constructor will throw. If you do not catch an checked exception in a method, that method must be declared to throw said exception. In the above example, I would recommend you deal with the exception in the catch clause, otherwise if something does go wrong you will have no way of knowing.
    Suggested reading: Lesson: Exceptions.

Similar Threads

  1. [SOLVED] How to declare an object of multi-dimension array?
    By FongChengWeng in forum Collections and Generics
    Replies: 7
    Last Post: January 14th, 2011, 01:17 AM
  2. [SOLVED] Method declaration in Java
    By mohsendeveloper in forum Object Oriented Programming
    Replies: 4
    Last Post: June 11th, 2009, 03:18 AM