mode 6 Flashcards

1
Q

What is an exception?

A

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.

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

What is….an error?

A

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

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

What is the hierarchy of exception handling in java?

A
(Throwable 	[class])
						|
					  /			\
					/			  \
		(Exception	[class])		(Error	[class])
			|
		(RuntimeException [class])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Keywords associated with exception handling in java?

A
  • try, catch, finally
  • throw
  • throws
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a “checked” exception?

A

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

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

What is an “unchecked” exception?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do we handle a checked exception

A

A try-catch block is how we handle a checked exception.

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

Can we have multiple catch blocks?

A

Yes, just make sure that specific exceptions go before general exceptions. The order matters.

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

What is the purpose of a try block?

A

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.

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

can we catch a RuntimeException?

A

Yes

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

CAN I THROW AN ERROR? AND CAN I CATCH AN ERROR?

A

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

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

Aside from try-catch, is there another way to handle exceptions in Java?

A

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.

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

What is the finally block?

A

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.

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

Understanding a stacktrace

A

Format of the stack trace:

Exception in [thread] [package].[class] : [additional message]
at [package1].[method1] ([file-name1] : [line number] )

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

When was finalize() method used?

A

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

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

Will the finally block ALWAYS run?

A

TECHNICALLY there are cases where the finally block won’t run:

    1. System.exit();
    1. System failure/fatal error
17
Q

What are the dos and don’ts of try-catch block

A

We MAY have:

  • a try-catch block
  • a try-finally block (as long as the try doesn’t contain a checked Exception)
  • a try-catch-finally

We may NOT have:
-a try block by itself

18
Q

What is a lambda?

A

a lambdas is essentially a disembodied function….

19
Q

How are lambda expressions formed?

A

Lambda expressions create an instance of a functional interface on the fly, using an anonymous method implementation.

20
Q

LAMBDA FORMAT:

A
FunctionalInterface myInter =	(parameter-list) ->  {
		 						//logic				};
21
Q

Use cases for lambdas?

A

comparable, comparator, runnable, etc.