File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Basic question about Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Basic question about Exception" Watch "Basic question about Exception" New topic
Author

Basic question about Exception

Golam Newaz
Ranch Hand

Joined: Jan 08, 2001
Posts: 64
Hi,
Regarding Exception. We know that when something went
wrong in the program then it throws Exception and we
catch that Exception using try {} and catch{} block for
the flow of program.
I found that same Exception, we try to catch either
public static void main ( String argv[]) throws IOException{
//
}
OR using try block like this:
try{
//
}
catch(IOException e){
System.out.println("...");
}

My question is in when and in what situation implicitly we would use to catch the Exception by using either of above.
can anybody explain,
Golam Newaz
------------------
Anshuman Acharya
Ranch Hand

Joined: Jan 19, 2001
Posts: 144
the situation to use try, catch block would be when you want to catch and also "process" the event there and then.
throws implies that "i am just throwing such and such exception... whoever calls me, deals with them"
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
let's say you trying to open a file which doesn't exist on your system. So it trows an exception. If you are using trows clause it means you don' twant to deal with it let it be taken care by something else. But if you want to get more control on it , you'd use a try / catch block to work around it. You would maybe give a user to locate the file and open it. It's nice option to have.


Val SCJP <BR>going for SCJD
Golam Newaz
Ranch Hand

Joined: Jan 08, 2001
Posts: 64
Thanks for your explanation. I understood but need more
specific usage. Look i am working with sound files. It
is on running on webbangladesh.com/game/home.html. It
is only au files. And for every file, i had to use
try{}catch{} block when i tried to load the sound file
individually. Right now it only 6 files. But if i
use 100 or more more files then every moment i have to use
try{} and catch{} block to catch the exception.
Can't i use throws IOException at a time instead of that
and in how?
need a example.
Golam Newaz
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
Definately not 100 catch / try blocks. You must rethink your design a little there. Can you design a class which will only load the sound files ? So you can have a method which will be processing that functionality so the only place where you can have that try / catch stuff in it. It be much better because it be all in one place.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Basic question about Exception