mode 6 Flashcards
What is an exception?
An exception is an exceptional event (aka an issue) that arises INSIDE of your program’s logic. More than likely the programming logic is fault or dangerous or careless.
What is….an error?
An exception is an exceptional event (aka an issue) that arises OUTSIDE of your program’s logic. More than likely the issue with with your runtime environment or system.
>examples:
>StackOverflowError
>OutOfMemoryError
What is the hierarchy of exception handling in java?
(Throwable [class]) | / \ / \ (Exception [class]) (Error [class]) | (RuntimeException [class])
Keywords associated with exception handling in java?
- try, catch, finally
- throw
- throws
What is a “checked” exception?
The compiler will give you a syntax error if you do NOT “handle” the logic that could potentially throw an exception/error.
In short, when you do NOT handle an exception it will lead to a syntactical error, the syntax error itself is NOT the exception being thrown.
example:
FileNotFoundException
IOException
What is an “unchecked” exception?
The compiler will NOT force you to “handle” the logic that could potentially throw and exception/error. meaning there will be no syntax error created.
Note: if you EVER see an unchecked exception YOU need to refactor your code, because you as the developer have made a mistake or was careless
example:
IndexOutOfBoundsException
InputMismatchException
How do we handle a checked exception
A try-catch block is how we handle a checked exception.
Can we have multiple catch blocks?
Yes, just make sure that specific exceptions go before general exceptions. The order matters.
What is the purpose of a try block?
The purpose of a try block is to attempt volatile code. You “try” the volatile code and if the volatile code behaves poorly then you have a “catch” block with some fallback, less volatile, code.
can we catch a RuntimeException?
Yes
CAN I THROW AN ERROR? AND CAN I CATCH AN ERROR?
Yes, but there’s no need to since the Error only informs about the existence of the error but there’s no way for us to do anything meaningful about it
Aside from try-catch, is there another way to handle exceptions in Java?
Yes, using the throws keyword
We can use the “throws” keyword…aka a concept called “ducking”
It’s called “ducking” because we’re ducking responsibility
When we use the “throws” keyword, the invoking method now has to “handle” the exception, we no longer need to.
What is the finally block?
The objective of the finally block is to run its logic regardless of whether or not there is an exception thrown.
The finally is often used as a place to put clean up logic, like cleaning up memory, closing streams, etc.
Understanding a stacktrace
Format of the stack trace:
Exception in [thread] [package].[class] : [additional message]
at [package1].[method1] ([file-name1] : [line number] )
When was finalize() method used?
We USED to use the finalize() method, which is called while the garbage collector is disposing of your objects. But now, we just use the finally block