4

I am wondering when is the best time to have a customized exception?

Code

public void myMethod() throws MyCustomizedException {

   try{
      ...
   }catch(IOException e){
     logger.log(e);
   }
}

caller.myMethod();
Jack
  • 6,430
  • 27
  • 80
  • 151
  • http://stackoverflow.com/questions/7962310/user-defined-exceptions-when-do-we-use-them-what-is-an-exceptional-situation – PSR Oct 14 '14 at 07:08

7 Answers7

1

The answer is really depends. Customized exception are project specific. Just for an example, consider I'm working upon a warehousing product.

public void addProductToWarehouse() throws WareHouseCapacityExceedException{
 if(warehouse.isFilled()){
     throw new WareHouseCapacityExceedException("Some custom message");
  }
  // proceed, warehouse is free.
}
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

The most common reason why one needs to create a cusomized exception is if you are building your own set of java Library and that there are other attributes and methods you want to include in your new Exception aside from those that are in the existing Exceptions.

Abel Callejo
  • 13,779
  • 10
  • 69
  • 84
0

Depends on where are you implementing it. I would say almost all the time!

There are different types of errors you would want to throw for your application in case somethign goes wrong. you will Implement custom exceptions in that case.

for example

In your code you are validating user input data, if valid data is not entered you would throw InvalidInputException (just an example)

you can not always throw exceptions pre defined in java, because those are defined with some reason. like NullPointerException this will be thrown when you try de-reference a null referance

dev2d
  • 4,245
  • 3
  • 31
  • 54
0

Before creating a custom exception, first always look at the ones included in the standard library and reuse them if its purpose looks identical (or close to identical) to yours.

See the direct known subclasses of Exception and RuntimeException. Some Exceptions also have many specialized subclasses, for example look at the direct subclasses of IllegalArgumentException or IllegalStateException.

For example if your method takes a parameter which cannot be null, use NullPointerException. If the parameter has a valid range and you don't accept other values, use IllegalArgumentException.

Create a custom Exception if there is no existing one in the standard library (or in the libraries you use) for its purpose or if the Exception you want to throw should be differentiable from the Exceptions that may be thrown by the code and methods you call from your method.

icza
  • 389,944
  • 63
  • 907
  • 827
0

The oracle java tutorials on Creating Exception Classes

Has this to say:

You should write your own exception classes if you answer yes to any of the following questions; otherwise, you can probably use someone else's.

  • Do you need an exception type that isn't represented by those in the Java platform?
  • Would it help users if they could differentiate your exceptions from those thrown by classes written by other vendors?
  • Does your code throw more than one related exception?
  • If you use someone else's exceptions, will users have access to those exceptions? A similar question is, should your package be independent and self-contained?

Additionally, I'd like to add that you should generally throw an exception if the current scope does not have enough information to effectively handle the exception. When you throw an exception, you pass it up the stack to be handled in a different scope.

Justin
  • 1,972
  • 13
  • 28
0

One use case is when you want to handle multiple exceptions in a generalized way. Example : Assume you want to SQLException, ConnctionException, SomeOtherException in a general way (show error to user on the scren for all 3 exceptions), then just wrap that exception in your Custom Exception and let the parent handle it in a general way. This prevents cluttering of code with multiple catch blocks ` for exceptions that have to be handled commonly.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

I'd say when you want to know exactly what went wrong. If it's your own custom exception, you know there's no other possible causes for it, as opposed to something generic or already-existing, which could have been thrown by an unrelated problem.

BIU
  • 2,340
  • 3
  • 17
  • 23