When would you rather declare an exception rather than handle it in Java?

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

My question is, can anyone provide an instance where this is usually done where you want the caller to handle the exception instead of the current method?

Edit: I meant calling method instead of superclass on the last line.

+2


a source to share


8 answers


In general, I would say, create your own exception stream so that the exception gets caught on code that can actually take the appropriate action .

This usually means that in a "library" method (or some common utility method in a large project), you will throw exceptions without catching them.



On the other hand, if you have a situation where you find that your method is throwing an exception that you think will almost never happen (for example, serialization usually includes all sorts of false checked exceptions that will not in practice, for example, if you deserialize an Integer it is unlikely that there is no Integer class, but you still have to catch the corresponding exception), then you have a third option for re-cropping in a RuntimeException.

+3


a source


I think "superclass" means the code called by your method.

If you expect the caller to know more about the problem compared to your method, you can delegate that responsibility to the calling stack.



In general, if you don't know how to deal with this problem, then don't do it. Ether passes it on or ends it on another exception.

+2


a source


If you can't handle the error at the point in a reasonable way (like displaying an error message, taking an alternate path, etc.) then just let it bubble up.

+1


a source


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

Here is an example of semi-concrete. Let's say I have a web application that is implemented as a series of states and actions. Suppose that when the state is being processed, database access triggers a flush SQLException

. This will not happen during normal operation of my application; this will only happen if there is something wrong with the database.

The method that accesses the database doesn't need to know what my procedure is to handle semi-phalactic errors like this. This logic is implemented at a higher level - in a method handling state - and it is essentially the same for any runtime error, literally RuntimeException

or not: it spits out a nice error message to the user who tells them to contact tech support.

Imagine if I had to add a try

/ block catch

that executes this logic for every method that accessed the database and could throw it away SQLException

. This is what is called nightmare

.

+1


a source


I used predictions as part of communicating information between the architectural layers of the application.

For example:
if the DAO (Database Access Layer) receives an SQLException, it will throw a custom DAOLayerException that is caught by the business word, which in turn throws an exception that hits the presentation layer.
This was for unchecked exceptions.

I usually follow the practice of throwing checked thrown exceptions (without handling them) if a function needs to be used by multiple callers or is unknown during function design.

+1


a source


In web frameworks (like spring) you often allow errors to propagate and the container will handle them (by displaying an error page or message to the user, rolling back a transaction, etc.).

Also, there are a lot of java errors that you will never catch, like OutOfMemoryError. You can catch them, but you cannot handle them properly.

0


a source


You asked for an example and here's a generic one.

If you write code to read a specific file format, they usually throw an IOException. The reason for this is that it can be used by a desktop application (which wants to deliver a dialog box) or a text utility (which wants to log an error message) or a web application (which wants to return an error code) Exception Handling allows this to be done.

0


a source


Yes, I would declare that it propagates until the parent call method is called (display in UI, break there ...)

In the case of an instance, I can use the helper methods in the sendEmail method. If the helper method says checkEmailAddress () threw an exception, I would like sendEmail to know about it, that it might propagate or issue a warning in the UI "email is invalid"

0


a source







All Articles