2

I know we can declare the exception for our method if we want it to be handled by the calling method. This will even allow us to do stuff like write to the OutputStream without wrapping the code in try/catch block if the enclosing method throws IOException.

My question is: Can anyone provide an instance where this is usually done where you'd like the calling method to handle the exception instead of the current method?

Edit: I meant calling method instead of super class in the last line.

Raidri
  • 17,258
  • 9
  • 62
  • 65
Epitaph
  • 3,128
  • 10
  • 34
  • 43
  • Something is not very clear: when you are writing a class usually you don't have the option of having the superclass or base class deal with exceptions for you. Normally superclasses are already written and you have to take them as they are. Maybe you meant the calling class? – Fabio de Miranda May 13 '10 at 16:03
  • 1
    @Fabio: I read "super class" as "calling method" there, since that's what he's talking about in the first graf. – pkh May 13 '10 at 16:04

8 Answers8

3

In general, I would say design your exception flow so that the exception is caught by the code that can actually take appropriate action.

This usually means that in a "library" method (or, some kind of general utility method in a large project), you will be throwing exceptions not catching them.

On the other hand, if you have a situation where you find yourself declaring your method to throw an exception that you believe in practice will hardly ever occur (e.g. serialisation generally involves all sorts of spurious checked exceptions which in practice won't occur, e.g. if you're deserialising an Integer, it's really unlikely that the Integer class is not present, but you still have to catch the appropriate exception), then you have a third option of re-casting to a RuntimeException.

Neil Coffey
  • 21,615
  • 7
  • 62
  • 83
2

I guess by "super class" you mean the code that called your method.

If you expect the caller to know more about the problem compared to your method, than you can pass this responsibility up the calling stack.

In general, if you do not know how to handle the problem, don't. Ether pass it or wrap it in another exception.

Georgy Bolyuba
  • 8,355
  • 7
  • 29
  • 38
1

If you can't handle the error at the point in a sensible way (e.g. displaying error message, taking an alternative path, etcetera), then just let it bubble up.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

If you want the error to be handled at a different level of the application.

Here's a semi-concrete example. Let's say I've got a web application which is implemented as a series of states and actions. Suppose that, while a state is being processed, a database access causes an SQLException to be thrown. This would not happen during the normal operation of my application; it would only happen if there were something wrong with the database.

The method that access the database doesn't need to know what my procedure for handling semi-fatal errors like this is. That logic implemented at a higher level — in the method that processes the state — and it's essentially the same for any runtime error, whether it's literally a RuntimeException or not: spit out a nice-looking error message to the user telling them to contact tech support.

Imagine if I had to add a try/catch block performing this logic to every method that accessed the database and could possibly throw an SQLException. That's what's called a nightmare.

Syntactic
  • 10,721
  • 3
  • 25
  • 25
1

I have used expections to as part of transferring information between architectural layers of application.

For Example:
if the DAO (Database Access Layer) gets a SQLException it throws a customized DAOLayerException which is caught by the businesslayer which in turn throws an exception which is caught by the presentation layer.
This was for unchecked exceptions.

I usually follow the practice of throwing checked unchecked exceptions (not handling them) if the function is to be used by numerous caller or are unknown at the time of development of the function.

frictionlesspulley
  • 11,070
  • 14
  • 66
  • 115
  • Most people don't like checked exceptions, makes for confusing flow. http://c2.com/cgi/wiki?TheProblemWithCheckedExceptions – Ruan Mendes Dec 05 '12 at 03:11
  • I think I meant unchecked exceptions. since the line besides this I explicitly specified *not handling them*. I will update my answer. Thanks for the heads up – frictionlesspulley Dec 05 '12 at 15:16
0

In web frameworks (like spring) you often let errors propagate and container will deal with them (by showing error page or message to the user, rolling back transaction, etc).

Also, there are lots of java errors you never catch, like OutOfMemoryError. You can catch them, but you can't deal with them properly.

Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181
  • I believe that it is implicit in the OP's question that he means checked exceptions, as you do not need to explicitly declare your methods as throwing unchecked exceptions. – danben May 13 '10 at 16:09
  • I see no difference with regard to my post, whether exception is checked or not. – Nikita Rybak May 13 '10 at 16:28
0

You asked for an example, and here is a common one.

If you are writing code to read a particular file format, these normally declare IOException. The reason for this is that it might be used by a desktop app (which wants to put up a dialog box) or a text utility (which wants to write an error message) or a web app (which wants to return an error code). Exception handling allows you to do that.

DJClayworth
  • 26,349
  • 9
  • 53
  • 79
0

Yes, I would declare it so it propagates up until a parent calling method which would actually handle it (display on UI, break there, ..)

For Instance, I may have some helper methods in a sendEmail method. If a helper method say checkEmailAddress() produced an exception, I would want sendEmail to know about it, which can further propagate it up or do an alert on UI "email is wrong"

Rk R Bairi
  • 1,289
  • 7
  • 15
  • 39