N
N
NoMoneyException2016-07-13 16:17:15
Java
NoMoneyException, 2016-07-13 16:17:15

Should I use exceptions or if/else?

Hello! Please tell me where to read / see in what situations to use exceptions and how it affects performance. And I don’t quite understand why, for example , use this:

public int hashCode() {
        try {
            return name.hashCode();
        }
        catch (NullPointerException exception){
            return 0;
        }
    }

If you can do it?
public int hashCode() {
        return (name!=null)?name.hashCode():0;
    }

Thank you!

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Alexey S., 2016-07-13
@eugene_leshchinskiy

examples are not equivalent
1. an error occurs in cases where "oops", which should not even be in theory. (that is, this error is for the programmer, not the user)
2. here you won’t even know that “something went wrong” or you will have to check for a 0 response at a higher level and react to it

S
Saboteur, 2016-07-13
@saboteur_kiev

These are not comparable things.
if/else checks for an error at the specified location.
Exception will intercept it inside any nesting level, and will not allow the program to complete if somewhere inside there is a critical error that you are intercepting.
That is, if you forgot to put if / else somewhere, the result will be a sudden termination of the program.
exception can be set at the highest level, and ensure that a specific window will be closed, some specific operation will not be performed, but the application will continue to work and perform other functions.
Depends on what you are doing. If the toy has 2 buttons, then it is possible without exceptions, but if it is a multi-user application, where, due to the unforeseen action of one user, the entire application for all users will fall, if / else will obviously not be enough.

B
bromzh, 2016-07-13
@bromzh

Please tell me where to read/see

Kay Horstmann, Gary Cornell "Java. The Professional's Library". 9 edition. Volume 1. Chapter 11.
In particular, there is a section titled "Exception Handling Guidelines" where everything is clearly explained.

A
Andrey Shishkin, 2016-07-13
@compiler

The first option somehow smacks of idiocy. And where is he from?

A
Alexey Cheremisin, 2016-07-13
@leahch

Gentlemen, I won’t take something into my mind ... I always thought that Exception is evil, but then I decided to check it. I understand that the JVM can optimize something ...

Oops! Everything fell into place!
IF execution time: 1231
EXCEPT execution time: 1260

package jtests;

import java.util.HashMap;

public class MyTest2 {

  String testIf(HashMap<String, String> h, int i) {
    String r = "None";
    if(h.containsKey("NONEKEY")) {
      r = h.get("NONEKEY");
    }
    return r;
    
  }
  String testExept(HashMap<String, String> h, int i) {
    String r = "None";
    try {
      r = h.get("NONEKEY");
    } catch(Exception e) {
      return e.getMessage();
    }
    return r;
    
  }
  public static void main(String[] args) {
    MyTest2 m = new MyTest2();
    HashMap<String, String> h = new HashMap<String, String>();
    h.put("KEY", "VALUE");
    
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < 900000000; i++) {
      m.testIf(h,i);
    }
    long endTime = System.currentTimeMillis();
    System.out.println("IF execution time: " + (endTime - startTime) );
    
    startTime = System.currentTimeMillis();
    for (int i = 0; i < 900000000; i++) {
      m.testExept(h,i);
    }
    endTime = System.currentTimeMillis();
    System.out.println("EXCEPT execution time: " + (endTime - startTime) );
  }

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question