File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes What kind posible exception thrown Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What kind posible exception thrown" Watch "What kind posible exception thrown" New topic
Author

What kind posible exception thrown

Ahamed Ali
Greenhorn

Joined: Apr 21, 2005
Posts: 19
Hi,

I would like to know, this code throws which kind of exceptions.

class X
{
private Vector objects = new Vector();

public void compute ()
{
Iterator iter = objects.iterator();
while (iter.hasNext()) {
try {
Object o = (Object)iter.next();
System.out.println(o.hashCode());
}
catch (Exception ex) {
// under which circumstances will which Exceptions
// be caught here ?
}
}
}

public void addObject (Object o)
{
objects.add(o);
}
}


Thanks in advance
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
one (crude, but functional) way to find out is to remove the "catch" statement and block, replace it with an empty "finally" statement/block, and try to compile the code. it will not compile, but the compiler will give errors for any checked exceptions that might arise from the "try" block. reading those errors will tell you what you need to worry about, at minimum.

...of course, there's still the possibility of unchecked exceptions being thrown. you'll have to read the documentation for any methods and classes you make use of inside the "try" block to learn about those.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What kind posible exception thrown