java.lang.RuntimeException

Why should not java.lang.RuntimeException be used? Is it because it’s a class that has a subclass tree?

It’s an exception whose derived exceptions are a direct product of bad programming practices

AnnotationTypeMismatchException, ArithmeticException, ArrayStoreException, BufferOverflowException, BufferUnderflowException, CannotRedoException, CannotUndoException, ClassCastException, CMMException, ConcurrentModificationException, DataBindingException, DOMException, EmptyStackException, EnumConstantNotPresentException, EventException, FileSystemAlreadyExistsException, FileSystemNotFoundException, IllegalArgumentException, IllegalMonitorStateException, IllegalPathStateException, IllegalStateException, IllformedLocaleException, ImagingOpException, IncompleteAnnotationException, IndexOutOfBoundsException, JMRuntimeException, LSException, MalformedParameterizedTypeException, MirroredTypesException, MissingResourceException, NegativeArraySizeException, NoSuchElementException, NoSuchMechanismException, NullPointerException, ProfileDataException, ProviderException, ProviderNotFoundException, RasterFormatException, RejectedExecutionException, SecurityException, SystemException, TypeConstraintException, TypeNotPresentException, UndeclaredThrowableException, UnknownEntityException, UnmodifiableSetException, UnsupportedOperationException, WebServiceException, WrongMethodTypeException

If the size of the buffer is verified before anything else, it is prevented a BufferOverflowException and a BufferUnderflowException

If it is verified that the file exists before any operation is done, then FileSystemNotFoundException is never going to happen

If you verify for null before referencing something, then NullPointerException is avoided

If the array is verified to have elements, an IndexOutOfBoundsException is prevented

And we could continue with each one of the exceptions quoted before. So the use of java.lang.RuntimeException should be avoided because it contains exceptions that are solved by programming defensively.

What one should do is:

try
  index
catch IndexOutOfBoundsException // no RuntimeException
  log_error
  handle_error

or even better

if len(array) == 1:
  array[0]
else:
  log_empty_array
  handle_empty_array
2 Likes