Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
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?
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
- Exception - if not handled by a try/catch or using a throws keyword it will cause a compilation error
- 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üller1,089 Points
I assume the question is: Why are RunTimeErrors harder to catch than CompileTimeErrors?
First, what is the difference between them?
- RunTimeErrors: Occur while the program is running, often errors of logic
- CompileTimeErros: Occur before the program even started. Often errors of syntax etc.
What do you think is harder to catch?
- To fix an error of logic like divided by zero?
- 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.