Exception Handling?

I have studied Exception handling .I know we use exception at places where we think are chances of error,But still i am not able to put exceptions accurately..can someone suggest some specific method to put exception handling in files?
[242 byte] By [girl86a] at [2007-11-27 3:54:35]
# 1
Are you trying to make your own method, or are you looking for an existing method that throws an Exception?
CaptainMorgan08a at 2007-7-12 8:58:43 > top of Java-index,Java Essentials,New To Java...
# 2
i am looking for the existing methods only..what can be done for proper implementation of exception handling?i mean how to identify where to place an exception?
girl86a at 2007-7-12 8:58:43 > top of Java-index,Java Essentials,New To Java...
# 3

Look in the API to see if a method throws an Exception. For example, this constructor of the Scanner class:

[url]http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#Scanner(java.io.File)[/url]

Throws a FileNotFoundException.

try

{

Scanner s = new Scanner(new File("asdf.txt"));

}

catch(FileNotFoundException e)

{

e.printStackTrace();

}

CaptainMorgan08a at 2007-7-12 8:58:43 > top of Java-index,Java Essentials,New To Java...
# 4

hi,

if you want a general try catch block which can handle any exception then do the following thing.

//--

try{

// some code

}

catch (Exception e) {

e.printStackTrace();

}

//--

this can catch any exception because "Exception" is the super class of all exception classes( i.e, IOException, FileNotFoundException, SQLException etc).

but i think you should use specific exception handling. because

if you are trying to create a file in wrong directory, then you may want to handle ( suppose to show a specifice msg) it one way. and for failing to write into a file, want to show a different msg.

for this purpose you need to do the specific exection handling.

try

{

f = new RandomAccessFile("a.txt", "rw");

f.write(10);

}

catch (FileNotFoundException ex)

{

System.out.println("can not create file.");

}

catch (IOException ex1)

{

System.out.println("unable to write into file");

}

you can see the api documentation to know which methods throws which exception.

ashiq28a at 2007-7-12 8:58:43 > top of Java-index,Java Essentials,New To Java...