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

Exceptional question for exceptional behaviour

I was just wondering, what would happen if we didn't catch an exception? Would it stop the program when encountered or would the compiler stop us from even compiling if we do not add a try-catch block or something else maybe?

Any answer is appreciated :D

3 Answers

Hello

Please keep in mind that there are several type of exceptions ... or "errors"

Run time errors which occur during run time.

and

Compile time errors which are caught by the compilers at compile time ... before you even to get to run the program.

Now back to your question: "Would it stop the program when encountered or would the compiler stop us from even compiling if we do not add a try-catch block or something else maybe"

the answer is it depends on the type of the error.

if it is a compile time error/exception ... the compiler will stop you there ... at compile time. Example you forgot to the } closing in an if statement

if it is a run time error ... say you enter a zero from a command line and you divide a/0, and you did not catch this exception, an exception will be thrown and the program terminates immediatly.

If this answers your question, please mark the quation as answered.

Thank you

Ah yes! I had forgotten about the types of errors and was thinking that exceptions and errors were 2 different things :P

Thank you very much! This was definitely the easiest answer to understand :)

In my understanding, when an exception occurs program processing gets terminated and doesn’t continue further. In such cases we get a system error message.

Hmm... I was thinking the same thing

There are two categories of exceptions in Java, checked and unchecked.

Unchecked exceptions don't have to be caught or declared as thrown by the method. Unchecked exceptions are RuntimeException and any of its subclasses. Not handling them will not cause a compiler error.

Checked exceptions are any other subclass of Exception. These exceptions must be explicitly handled in a try/catch block or declared as thrown to let the next function up the chain handle them. Not doing so will cause an compiler error. IOException is an example of a checked exception.

Not catching an exception will terminate the program. This includes any checked exceptions you declare that main() throwns, since it's the last stop for checked expections.

Thank you so much!