Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Getting There Type Casting

Why are Runtime Exceptions hard to catch as opposed to compile time errors?

The video mentions that Generics were added to avoid runtime errors that show up when programmers forget to use the instanceof keyword before assign objects.

It also mentioned that Generics were added to address this problem because Runtime Errors are hard to catch. Are there any specific examples of why Runtime errors are hard to catch?

2 Answers

Compilation Errors happen before you even execute the code. If you type a bunch of nonsense like adfadglksjlnkbl this can't be compiled - .java to .class - which means that you can't execute it (run it)

Runtime Error is actually incorrect, it is Runtime Exceptions - these happen when programmers do something that is example of bad coding practice. Like trying to divide by 0 e.g. 10 / 0 - this is impossible and shouldn't ever be allowed. A programmer can avoid dividing by 0 by validating the denominator with a if n != 0. The compiler that is trying to convert this code from a .java file to a .class file can't always find these issues and will compile anyways but at runtime this will fail and throw a RuntimeException is it doesn't know what else to do.

There are 2 General Types of Exceptions

  1. Exception - if not handled by a try/catch or using a throws keyword it will cause a compilation error
  2. RuntimeException - compiler isn't award of it and will compile the code then it'll be thrown during program execution.

Exceptions are normal, they happen when code can't be validated beforehand e.g. When opening a file to read and write to/from it - we can't guarantee it'll work until we try. The compiler is aware of these unavoidable situations so it force us (developers) to provide a try/catch block or throws so the program knows what to do next in the situations where it can't connect that file.

Sandro Müller
Sandro Müller
1,089 Points

I assume the question is: Why are RunTimeErrors harder to catch than CompileTimeErrors?

First, what is the difference between them?

  1. RunTimeErrors: Occur while the program is running, often errors of logic
  2. CompileTimeErros: Occur before the program even started. Often errors of syntax etc.

What do you think is harder to catch?

  1. To fix an error of logic like divided by zero?
  2. To fix an error because you forgot a bracket?

Maybe the programmer is not aware that division by 0 is not defined. He could spent hour asking himself, why this is not working. If you forget a bracket, the compiler even tells you, which line you should look for.

The console output will still tell you the RuntimeException that is being thrown, including the case of a divide by 0. RuntimeErrors are not meant to be caught, at least in the sense of a try/catch block.