Exceptions Flashcards

1
Q

Exceptions

A
  • come in two forms:

- check and unchecked

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Checked Exceptions

A
  • The term “checked” means that the compiler will look for and catch the error, and you will not be able to run your application until it’s fixed
  • Exceptions: IOException, ClassNotFoundException
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Unchecked exceptions

A

An unchecked exception will slip through, and crop up in the middle of execution, potentially killing your application if not handled.

-Runtime exception : arithmeticException, ClassCastException, IndexOutofBoundsException
Error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Errors

A

-have nothing to do with your Java program (have to do with the JVM like running out of memory)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

try/catch block

A

-must have at least one catch block or final block

  • the try block contains code or method invocations which can throw exceptions.
  • The catch block grabs the thrown exception and executes the enclosed code.
  • You can have multiple catch blocks to handle different types of exceptions differently.
  • The finally block executes regardless of the outcome, even if there is a return statement in the try block.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Stacktrace

A

-a debug message that shows exception that occurred, its location in the code, and other lines of code affected by the exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Multiple catch blocks

A

-When catching multiple exceptions in Java, each catch statement will be evaluated in order to determine if it applies to the exception being thrown (top to bottom), and only the first applicable one will actually catch the exception.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Polymorphism tree principle example

A

-the principle of polymorphism means that every ArithmeticException is also a RuntimeException, which is also an Exception.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The throws clause

A
  • re-throw an exception up to the calling code
  • A method declaration can have a throws clause, indicating that it may throw and will not handle certain exceptions
  • a method with a throws clause is a source for checked exceptions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Handling Exceptions

A
  • handling exceptions costs a lot of CPU (processing) time
  • when performance is of utmost importance, it’s better to write code with no exceptions or risk failure
    ex: video games
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Exception

A

-Exception is a specific subclass of Throwable and it represents all possible exceptions that may occur within your program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

RuntimeException

A

parent class of all unchecked exceptions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to handle checked exceptions in code?**

A

-By using a try/catch/finally block or by rethrowing the exception using the throws keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly