Urbanleg
Urbanleg

Reputation: 6542

Exception - why even bother throwing?

So, I know this is going to sound a little extreme but I'm trying to make a point:

OK. so my code doesn't care what kind of exception is being thrown, 99.9% of the time if an exception is being thrown it handles it the same no matter what.

Now, Why should i even bother throwing \ creating new exception in my code? apparently all the libraries i use throw them already and the are very informative as well.

a null object passed for my method? who cares, a null pointer exception will be thrown automatically for me.

Can you make a good argument why should i create new exceptions and throw them?

EDIT what i mean:

why bother with this:

public myMethod() {

try { 
    doStuff1();
}
catch(Exception e) {
throw new Exception("Error in doStuff1");
}
try { 
    doStuff2();
}
catch(Exception e) {
throw new Exception("Error in doStuff2");
}

when i can use this:

public myMethod() {

doStuff1();
doStuff2();

}

Upvotes: 1

Views: 284

Answers (3)

P̲̳x͓L̳
P̲̳x͓L̳

Reputation: 3653

From Java tutorials - Advantages of Exceptions

  1. Advantage 1: Separating Error-Handling Code from "Regular" Code
  2. Advantage 2: Propagating Errors Up the Call Stack
  3. Advantage 3: Grouping and Differentiating Error Types

Answer

Exception handlers that are too general can make code more error-prone by catching and handling exceptions that weren't anticipated by the programmer and for which the handler was not intended.

As noted, you can create groups of exceptions and handle exceptions in a general fashion, or you can use the specific exception type to differentiate exceptions and handle exceptions in an exact fashion.

Upvotes: 0

Prabhaker A
Prabhaker A

Reputation: 8483

why should i create new exceptions and throw them?

In some cases it is common and good practice to throw Exception deliberately.
Take

IllegalArgumentException

If your method only accepts arguments within a particular range, e.g. only positive numbers, then you should check for invalid parameters and throw an IllegalArgumentException.

For example:

public int calculateFactorial(int n) {
  if (n < 0)
    throw new IllegalArgumentException("n must be positive");
  if (n >= 60)
    throw new IllegalArgumentException("n must be < 60");
  ...
}  
public void findFactorial(int n)
{
     try{
          calculateFactorial(n)       
      }catch(IllegalArgumentException ie){
         System.out.println(ie.getMessage());
       }
}

Upvotes: 0

Blake Hood
Blake Hood

Reputation: 314

Your client wants detailed error reporting. What went wrong, and where did it go wrong.

They don't have a clue what a null pointer exception is. And even then, that in itself is not particularly helpful without the stack trace. So a null pointer gets passed into your RetrieveReportX method? Check if it's null and throw a ReportXNoIdentifierException rather than letting it auto-throw a null pointer exception. Then you'll have a error handler somewhere that, based on these custom exceptions, can report exactly what went wrong in what (human) process, and your client is happy because rather than NullPointerException at [stacktrace], they can see "An identifier was not supplied when attempting to retrieve report X."

Upvotes: 1

Related Questions