Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

For example, I have a controller that loads a file and hands it over to the processing. Should I handle the exception in the file loader and return Null if something is wrong, or should I throw the exception and handle it in the controller? Without the file the rest of the program can“t work. Where should I handle a exception that shuts down the program properly?

I want to shut down an Android application properly.

share|improve this question

1 Answer

up vote 4 down vote accepted

You should not return null. The exception should be handled in the top-most layer.

Also, the program should not terminate, but give feedback to the user, unless it is a Unix style command line program where it's ok to terminate after showing an error message.

Besides, by definition no exception is fatal and each one can be handled.

Errors are fatal and cannot be recovered from.

Exceptions and errors are not the same.

share|improve this answer
1  
In practice, as opposed to definition, the difference between errors and exceptions is rather fuzzy. You can sometimes recover nicely even from a null pointer exception, which despite its usual name is normally considered an error. My best definition is that an "error" is an "exception" that takes too much work to catch; easier to stop it from happening than to recover from it. – RalphChapin Oct 3 '12 at 14:14
1  
In Java, some events cosidered errors (not Exceptions) are InternalError, OutOfMemoryError, StackOverflowError, UnknownError, etc. These situations prevent the virtual machine to even continue to run the application. These are surely conditions a program cannot recover from. It's like the program is a bus, an exception would be a tire exploding. An error would be the engine suddenly disappearing. – user61852 Oct 3 '12 at 17:28
They are defined as errors, and with very good reason. Still, they're just at one extreme of a continuum. I've wanted to catch OutOfMemoryError on occasion, and I recall a question on SO about how to do that reliably. (I don't think there was a good answer.) And people (though not me) complain about checked exceptions, meaning they want to treat them as errors. This is something of a fine point, and the terms "Exception" and "Error" do have (fairly) clear and distinct meanings. – RalphChapin Oct 3 '12 at 18:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.