Java Exceptions & GUI Flashcards
What is the difference between checked and unchecked exceptions? (T!)
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!)
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!)
Let’s repeat that…an exception is an object!
A major benefit of having an error signaled by an exception is this:
it separates the code that deals with errors (the exception handling code) from the code that is executed when things are running smoothly. (T!)
So what exactly is an Exception? (T!)
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!)
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).
This block has been written specifically to deal with that kind of problem. (T!)
An exception is always an object of some subclass of the standard class
Throwable
All the standard exceptions are covered by two direct subclasses of the class Throwable, the class ___ and the class ____
‘Error’ and the class ‘Exception’
Both these classes themselves have further subclasses which identify specific exception conditions.
Exception class hierarchy
Throwable ^ ^ Error Exception ^ Runtime
Error exceptions
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.
ThreadDeath and LinkageError
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.
VirtualMachineError
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.
Subclasses of Runtime Exceptions (unchecked, so you don’t have to catch ‘em!)
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.
Checked Exceptions: Java forces you to declare them or catch them…
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.
Checked Exceptions: Java forces you to declare them or catch them…
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.
Handling Exceptions
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…