1)
Do not hide exceptions
Exceptions are usefull. They are not something to be ashamed of and do not need to be hidden.
E.g.
try {
...
} catch (Exception e) {
//do nothing here
}
By doing this you just swallow all
Exceptions (including RuntimeExceptions). No one will ever notice that
something went wrong. Catch only those Exceptions you really want to
catch and you are capable (and willing) to handle at that time.
2)
Do not be too generic with your exceptions
E.g.
public void someMethod() throws Exception {
...
}
Try to tell the users of your code what might happen in your method. Don't be afraid of doing something like
public void someMethod() throws NoSuchElementException, ConcurrentModificationExceptio
n, IllegalArgumentException, InterruptedException {
...
}
Admitted,
this looks a bit extreme (and is just a random example), but it tells
what might happen and what the user of your code has to be prepared of.
3)
Do not be afraid of defining your own exceptions
There is a
huge amount of exceptions out there, but still, there might be none for
your special case. If you need an exception that e.g. happens when your
webcam isn't available then define a WebcamUnavailableException or
something alike.
Sometimes it is also a good idea to not use a fitting exception but to
create one that fits even more. E.g. you want to load an image taken by
your webcam which isn't there, a FleNotFoundException will perfectly
fit, however, a NoImageToLoadException will fit even more.
4)
Declaring Data Members as Public
Honsetly, there is no reason
to declare data members as public. Data members should be limited to
private (or protected when you really have to).
Accessing those members should be done using set\get methods only.
Interviews By Company :