When should the exception be thrown?

I am wondering when is the best time to create a custom exception?

the code

public void myMethod() throws MyCustomizedException {

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

caller.myMethod();
+4
source share
7 answers

The answer really depends. Individual exclusion depends on the specific project. As an example, consider how I work on a warehousing product.

public void addProductToWarehouse() throws WareHouseCapacityExceedException{
 if(warehouse.isFilled()){
     throw new WareHouseCapacityExceedException("Some custom message");
  }
  // proceed, warehouse is free.
}
+1
source

The most common reason you need to create a cusomized exception is to create your own set of java libraries and the presence of other attributes and methods that you want to include in your new exception, in addition to those that are in the existing Exception.

0
source

, . !

, , somethign . .

, , InvalidInputException ( )

, java, - . , NullPointerException, ,

0

, , , , ( ) .

. Exception RuntimeException. Exception , , IllegalArgumentException IllegalStateException.

, , null, NullPointerException. , IllegalArgumentException.

Exception, ( ) Exception, , Exception, , .

0

oracle java

:

, "" ; , , .

  • , , Java?
  • , , , ?
  • ?
  • , ? , ?

, , , , . , , .

0

- . : , SQLException, ConnctionException, SomeOtherException ( scren 3 ), . catch ` , .

0
source

I would say when you want to know exactly what went wrong. If this is your own exception, you do not know that there are no other possible reasons for it, unlike something common or already existing that could be caused by an unrelated problem.

0
source

All Articles