Printing the stack trace vs the exception itself

trevalexandro

quick question. Of course the stack trace gives way more information, but is it a bad practice to use the exception itself instead in some cases? Like just getting "null pointer exception" as opposed to this huge dump of stuff? If this doesn't make any sense, the two differences would be:

(Exception e)
{
  print e
}

AND

(Exception e)
{
  e.printStackTrace
}
Eray Balkanli

The difference between them is that print e returns the type of exception and error message while printStackTrace returns the whole stacktrace of the exception. printStackTrace is more beneficial to be used while debugging.

Example:

print e:

java.lang.IndexOutOfBoundsException

e.printStackTrace():

java.lang.IndexOutOfBoundsException: Index: 8, Size: 0
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)
    at com.o2.business.util.Trial.test(CommonUtilsTest.java:866)

printStackTrace might be good for programmer, but it is not readable and user-friendly for end users. As far as I know, printStackTrace prints the results in the default Errorstream: your console. For better practices, you can check the link: http://www.onjava.com/pub/a/onjava/2003/11/19/exceptions.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Code is not printing stack trace

From Dev

Code not printing entire stack trace

From Dev

Code not printing entire stack trace

From Dev

log not printing the full stack trace

From Dev

How to replace the global exception handler in JUnit 4.12 with one printing "root cause first" stack trace?

From Dev

Exception Stack Trace Missing Items

From Dev

Why is the exception stack trace not logged in?

From Java

How to print the stack trace of an exception object in Python?

From Dev

Exception that emails stack trace when raised

From Dev

locals() and globals() in stack trace on exception (Python)

From Dev

How to use a stack trace dump to debug an exception?

From Dev

Rethrow an Exception with correct line numbers in the stack trace

From Dev

How can an Exception be created/thrown with no stack trace?

From Dev

logback: newline before exception stack trace but not otherwise?

From Dev

Exception stack trace and aggregation in Parallel.ForEach

From Dev

Suppressing stack trace when catching exception

From Dev

How to print the stack trace of an exception object in Python?

From Dev

locals() and globals() in stack trace on exception (Python)

From Dev

How to use a stack trace dump to debug an exception?

From Dev

cxf null pointer exception without stack trace

From Dev

How does the console obtain the stack trace of an exception?

From Dev

Log4j not printing complete stack trace

From Dev

NUnit/Mono not printing stack trace line number even with --debug

From Dev

py.test printing out full data in stack trace

From Dev

Printing Exception vs Exception.getMessage

From Java

Get exception description and stack trace which caused an exception, all as a string

From Dev

How to print the selenium exception stack trace in Extent reports

From Dev

Why does android logcat not show the stack trace for a runtime exception?

From Dev

How to get stack trace string without raising exception in python?

Related Related

  1. 1

    Code is not printing stack trace

  2. 2

    Code not printing entire stack trace

  3. 3

    Code not printing entire stack trace

  4. 4

    log not printing the full stack trace

  5. 5

    How to replace the global exception handler in JUnit 4.12 with one printing "root cause first" stack trace?

  6. 6

    Exception Stack Trace Missing Items

  7. 7

    Why is the exception stack trace not logged in?

  8. 8

    How to print the stack trace of an exception object in Python?

  9. 9

    Exception that emails stack trace when raised

  10. 10

    locals() and globals() in stack trace on exception (Python)

  11. 11

    How to use a stack trace dump to debug an exception?

  12. 12

    Rethrow an Exception with correct line numbers in the stack trace

  13. 13

    How can an Exception be created/thrown with no stack trace?

  14. 14

    logback: newline before exception stack trace but not otherwise?

  15. 15

    Exception stack trace and aggregation in Parallel.ForEach

  16. 16

    Suppressing stack trace when catching exception

  17. 17

    How to print the stack trace of an exception object in Python?

  18. 18

    locals() and globals() in stack trace on exception (Python)

  19. 19

    How to use a stack trace dump to debug an exception?

  20. 20

    cxf null pointer exception without stack trace

  21. 21

    How does the console obtain the stack trace of an exception?

  22. 22

    Log4j not printing complete stack trace

  23. 23

    NUnit/Mono not printing stack trace line number even with --debug

  24. 24

    py.test printing out full data in stack trace

  25. 25

    Printing Exception vs Exception.getMessage

  26. 26

    Get exception description and stack trace which caused an exception, all as a string

  27. 27

    How to print the selenium exception stack trace in Extent reports

  28. 28

    Why does android logcat not show the stack trace for a runtime exception?

  29. 29

    How to get stack trace string without raising exception in python?

HotTag

Archive