Tell me by what method is it possible to put an exception message in a string? For example, something like this

java.lang.StringIndexOutOfBoundsException: String index out of range: -1

  • is it a message or a class name too? - Victor
  • exactly the message. I guess e.getMessage (), but the return is null . This, apparently, is because the exception itself is not forwarded (it did not work in sms) in the program and, accordingly, there is no message? - Prapor Pr
  • I suspect that if null means the message is not there. - Victor
  • Understood. Just the exception itself did not arise. - Prapor Pr

1 answer 1

You can get a brief description of the exception using the toString() method:

 int[] a = {1, 2, 3}; try { int b = a[10]; } catch(Exception e) { String exception = e.toString(); System.out.println(exception); // java.lang.ArrayIndexOutOfBoundsException: 10 } 

An example .