Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to catch the Exception vs When to throw the Exceptions?

I have been coding in Java for a while now. But sometimes, I don't understand when I should throw the exception and when should I catch the exception. I am working on a project in which there are lot of methods. The hierarchy is something like this-

Method A will call Method B and Method B will call some Method C and Method C will call Method D and Method E. 

So currently what I am doing is- I am throwing exceptions in all the methods and catching it in Method A and then logging as an error.

But I am not sure whether this will be the right way to do it? Or should I start catching exceptions in all the Methods. So that is why this confusion started in my- When should I catch the Exception vs When should I throw the exceptions. I know it's a silly question but somehow I am struggling to understand this major concept.

Can someone give me a detailed example of When to catch the Exception vs When to throw the Exceptions so that my concepts gets cleared on this? And in my case, should I keep on throwing the exception and then catch it in the main calling Method A?

like image 392
AKIWEB Avatar asked Sep 08 '13 00:09

AKIWEB


People also ask

Is exception should be thrown early and caught late?

Remember “Throw early catch late” principle. This is probably the most famous principle about Exception handling. It basically says that you should throw an exception as soon as you can, and catch it late as much as possible. You should wait until you have all the information to handle it properly.

Is it a good approach to throw an exception?

The short answer is NO. You would throw an exception if the application can't continue executing with the bad data. In your example, the logic is to display an error message on the front end and Option 2 is the cleaner method for achieving this requirement.

What is the purpose of throwing and catching an exception?

You generally catch an exception in a method when you want your program to continue running. You throw an exception when you want a higher level method that is calling that method to handle the exception instead. For example, you might throw it all the way back to your Main method, which has a try..

Should we throw exception from catch block?

The caller has to handle the exception using a try-catch block or propagate the exception. We can throw either checked or unchecked exceptions. The throws keyword allows the compiler to help you write code that handles this type of error, but it does not prevent the abnormal termination of the program.


1 Answers

You should catch the exception when you are in the method that knows what to do.

For example, forget about how it actually works for the moment, let's say you are writing a library for opening and reading files.

So you have a class, say:

public class FileInputStream extends InputStream {     public FileInputStream(String filename) { } } 

Now, lets say the file doesn't exist. What should you do? If you're struggling to think of the answer, that's because there isn't one... the FileInputStream doesn't know what to do about that problem. So it throws it up the chain, i.e.:

public class FileInputStream extends InputStream {     public FileInputStream(String filename) throws FileNotFoundException { } } 

Now, lets say someone's using your library. They might have code that looks like this:

public class Main {     public static void main(String... args) {         String filename = "foo.txt";         try {             FileInputStream fs = new FileInputStream(filename);              // The rest of the code         } catch (FileNotFoundException e) {             System.err.println("Unable to find input file: " + filename);             System.err.println("Terminating...");             System.exit(3);         }     } } 

Here, the programmer knows what to do, so they catch the exception and handle it.

like image 162
durron597 Avatar answered Sep 30 '22 15:09

durron597