Performance cost of coding "exception driven development" in Java? -


Is there any display cost to create, throw and hold exceptions in Java?

I am planning to add 'exception-driven development' in a big project, I want to design my exceptions and would like to include them in my ways, to help the developers catch up and do the right thing Will be forced into For example, if you have a method of obtaining a user from a database based on a name.

  public user getUser (string name);  

However, it is possible that the user may be null and forget to check it before using the user's public method.

  User User = GetUser ("Adam"); Int age = user.getAge ();  

The result will be nullion-exeption and crash. However, if I made a check before returning the user-object, then throw it off and tap 'UserIsNullException':

  Public user throws UserUsNullException (get string name);  

I force the applicant to think and act:

  try {user user = getUser ("adam"); Int age = user.getAge (); } Code (UserIsNullException E) {}  

This code makes it very safe for unexpected crashes and eliminates more bugs, assume that the website has hundreds of visitors per hour, and this design pattern Everywhere is used very much.

How would such a design approach affect? Do the profits go out of cost or is it just plain bad coding?

Thanks for any help!

Update! To be clear, my attention is not wrapping up a null pointer exception, as my example might suggest. The goal is to force the implementer to write an attempt / catch, because after avoiding the pain in the head of the real accident:

User == Tap

Forget I went. The question is to compare these two design models:

  int age; Try {user user = getUser ("Adam"); Age = user.getAge (); } Hold (UserIsNullException e) {age = 0; }  

versus:

  int age; User user = getUser ("Adam"); If (user! = Null) {age = user.getAge (); } And {age = 0; }  

"resulting in NullPointerException and an accident."

An NullPointerException is an exception, and can be caught in a catch.

Therefore, the extra exception is unnecessary unless it clarifies the code for clarity in this example, it is not really. In fact, you have taken an unwanted programmer error exception and promoted in a check exception.

Creating a check exception for the programmer's error. Actually your code is clearly assuming the probability that the programmer started an error when they did not.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -