Java Error & Exceptions Flashcards

1
Q

What is Exception ?

A

is a problem that arises during program execution.

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

Type of Exceptions

A

Checked (compiled) Exception
Unchecked(runtime) Exception

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

Java Error

A

Java errors are no exceptions at all , but problems that arises beyond control of programmer.
Examples :
*stack overflow error
*JVM out of memory

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

stack overflow error

A

Occurs when computer program tries to use more memory space at the call stack than what has been allocated to that stack

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

Java Exception hierarchy

A

All class exceptions are subclass of java.lang.Exception class.
Throwable class contain java exception class and another class called Errors

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

Exception two main subclasses

A

Exception class has two main subclasses:
*RuntimeException class
*IOException class

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

Some Exception class methods

A

*getMessage(): get details about error
*toString(): return name of class + getMessage() result
*printStackTrace():print toString() result + stack trace to System.err

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

try catch block

A

try{
//code that might throw error here
}catch(Exception e){
//handle Exception here
}

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

we can use multiple catch block

A

try {
// Protected code
} catch (ExceptionType1 e1) {
// Catch block
} catch (ExceptionType2 e2) {
// Catch block
} catch (ExceptionType3 e3) {
// Catch block
}

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

Throws/Throw keywords

A

if a method doesn’t handle a compilation time exception , it must declare it using “throws” keyword
to throw an exception we use “throw” keyword
=>Throws vs Throw : throws postpone/delay the handling of checked exception ,however, throw invoke an exception explicitly.

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

Finally Block

A

*Always follows try or catch block
*execute no matter what happens to protected code(try block code)

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

Finally Block Syntax

A

try {
// Protected code
} catch (ExceptionType1 e1) {
// Catch block
} catch (ExceptionType2 e2) {
// Catch block
} catch (ExceptionType3 e3) {
// Catch block
}finally {
// The finally block always executes.
}

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

Rules of try catch finally block

A

*catch clause cannot exist without try statement
*finally is not necessary for try/catch block
*a try block cannot exist without catch or finally block
*no code can exist between try,catch and finally blocks

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

try-with-resources

A

when we use any resources like streams and connections… we have to close them explicitly with finally block.
try-with-resources also referred as automatic resource management automatically closes resources with try/catch blocks(without need to do it explicitly with finally block).

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

Syntax of try-with-resources

A

try(FileReader fr = new FileReader(“file path”)) {
// use the resource
} catch () {
// body of catch
}
}

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

Code before vs after try-with-resources

A

goto : https://www.tutorialspoint.com/java/java_exceptions.htm

17
Q

Java Built-in Exceptions

A

are exceptions defined by Java

18
Q

Rules for User-defined exceptions

A
  • All exceptions must be a child of Throwable class
  • to creacte a checked exception you must extends Exception
  • to create a Runtime Exception you must extend RuntimeException
19
Q

Example of User-defined/Custom Exception

A

goto: https://www.tutorialspoint.com/java/java_exceptions.htm