Java Exceptions & GUI Flashcards

1
Q

What is the difference between checked and unchecked exceptions? (T!)

A

if it is an unchecked exception, then Java does not force you to write any code to deal with it.
Conversely, if an exception is a checked exception, then Java does force you the coder to write some code to deal with it.

Exceptions that are sub classed from either the Error class the Runtime class are known as “unchecked exceptions”.(T!)This means that you the coder DO NOT have to specifically include exception handling code (try and catch blocks) for these types of exceptions. (T!)

All of the subclasses of Error class and all subclasses of Runtime class are unchecked exceptions. (T!)

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

An exception is an object that signals that an error of some kind has occurred in your program and bad things will happen (i.e. a crash) to your program if you don’t deal with it. (T!)

A

Let’s repeat that…an exception is an object!

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

A major benefit of having an error signaled by an exception is this:

A

it separates the code that deals with errors (the exception handling code) from the code that is executed when things are running smoothly. (T!)

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

So what exactly is an Exception? (T!)

A

One more time… an exception is an object that is created by the JVM when an abnormal situation arises in your program.
This object has data members that store information about the nature of the problem.(T!)

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

The exception is said “ to be thrown” , or tossed as an argument to a specific piece of code called a catch block (think of it as baseball player with a glove).

A

This block has been written specifically to deal with that kind of problem. (T!)

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

An exception is always an object of some subclass of the standard class

A

Throwable

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

All the standard exceptions are covered by two direct subclasses of the class Throwable, the class ___ and the class ____

A

‘Error’ and the class ‘Exception’

Both these classes themselves have further subclasses which identify specific exception conditions.

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

Exception class hierarchy

A
Throwable
  ^              ^
Error   Exception
                  ^
            Runtime
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Error exceptions

A

exceptions defined by the class ‘Error’, and it’s subclasses, all represent conditions that you as a coder are not expected to do anything about.therefore we don’t usually try to catch them, because there just is not really anything we can do with them anyway. They usually involve some sort of catastrophic failure in the JVM.
Examples: three direct subclasses of ‘Error’ are:
ThreadDeath, LinkageError, VirtualMachineError.

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

ThreadDeath and LinkageError

A
A ThreadDeath exception is thrown whenever an executing thread is deliberately stopped.
in order for the thread to be destroyed properly you should not catch this exception.
  When a ThreadDeath exception is thrown and not caught, it’s the thread that dies, not the program, so we’ll let this one pass us by.
LinkageError: this exception class has subclasses that record serious errors with the classes in your program.  
If there are incompatibilities between classes within your program, or one class tries to create an object of a non-existent class type then this type of exception could be thrown.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

VirtualMachineError

A

has four subclasses that specify exceptions that will be thrown when a catastrophic failure of the Java Virtual Machine occurs.
You are not prohibited from trying to deal with these kinds of exceptions, but in general, there’s little point in trying to catch them. When the JVM croaks, there isn’t much you can do…
Most common cause of this type of error is if the JVM tries to write to a bad memory cell in your RAM. It seizes up, says “I’m goin’ down!” and throws the exception to let the coder know what happened.

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

Subclasses of Runtime Exceptions (unchecked, so you don’t have to catch ‘em!)

A

ArithmeticException - An invalid arithmetic condition has arisen such as an attempt to divide by zero.
IndexOutOfBounds - You’ve attempted to use an index that is outside the bounds of the object it is applied to. Seen commonly with array processing and off-by-one errors in the for loop counter.
NegativeArraySize - You’ve tried to define an array with a negative dimension.
NullPointer - You have referenced an object variable that points to an object that does not currently exist in memory. Most common cause is that the object was never created. A second cause is that it was de-referenced and its variable name was assigned to some other object.
ArrayStore - You’ve attempted to store an object in an array that isn’t permitted for the array type.
ClassCast - You’ve tried to cast an object to an invalid type.
IllegalArgument - You’ve passed an argument to a method which doesn’t correspond with the parameter type i.e. you passed a String where a double was expected.
Security - Your program has performed an illegal operation that is a security violation.

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

Checked Exceptions: Java forces you to declare them or catch them…

A

rest of the exceptions that are represented by the subclasses of the ‘Exception’ class (omitting the Runtime class) are known as “checked exceptions”.
Option 1: you can “claim” or “confess” to the almighty JVM and declare that your block of code could possibly throw “this_type” of exception. To do this you use the “throws” keyword in the header of the method that contains the offending code.
Option 2: If you don’t claim the checked exception, you must include exception handling code in try and catch blocks in your program if your program code is capable of throwing such an exception.

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

Checked Exceptions: Java forces you to declare them or catch them…

A

rest of the exceptions that are represented by the subclasses of the ‘Exception’ class (omitting the Runtime class) are known as “checked exceptions”.
Option 1: you can “claim” or “confess” to the almighty JVM and declare that your block of code could possibly throw “this_type” of exception. To do this you use the “throws” keyword in the header of the method that contains the offending code.
Option 2: If you don’t claim the checked exception, you must include exception handling code in try and catch blocks in your program if your program code is capable of throwing such an exception.

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

Handling Exceptions

A

three kinds of code block you can use inside a method to handle exceptions
try block: It encloses code that may give rise to one or more exceptions. Code that can throw a checked exception that you want to catch must be placed inside a try block.
catch block :It encloses code that is intended to handle exceptions of a particular type that may be thrown in a try block.
finally block: A neat feature in Java. This code is always executed before the method ends, regardless of whether any exceptions are thrown in the try block. It is often used to hold “clean-up” code that can do things like closing a network or database connection before things really start going south…

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

The finally block

A

NOTE:A finally block is always executed regardless of what happens during the execution of the method. (T!)

17
Q

Important concepts to remember(T!)

A
Exceptions identify errors that arise in your program.
Exceptions are objects of subclasses of the class Throwable.
Exceptions may be “checked” or “unchecked”. Unchecked exceptions DO NOT need to be claimed or declared. Checked exceptions do need to be claimed. 
to handle an exception in a method, place the code that may generate the exception in a try block.
Exception handling code is placed in a catch block that immediately follows the try block.
You can define your own exception classes which in general should be derived from the class Exception.