5. 3
Unchecked exceptions are generally due to a glitch(es) in the code that are not required
to be handled. These exceptions are referred to as, unchecked exceptions, because these
glitches are unknown prior to runtime and do not force the programmer to establish an error
handling policy. It is actually recommended that these be thrown to the JVM, so it will print a
stack trace to help locate and correct the exception thrown.
For example, if one wrote a program and used a variable that was never assigned, the
programmer would see a NullPointer Exception. Also, unchecked exception might cause the
program to crash, instead of Java prompting the programmer to handle the exception thrown.
These kinds of exceptions are mostly prevented with solid programming practices. Below, is a
coding example of an unchecked exception, this is due to the variable being divided by 0.
Coding Example:
class Main {
public static void main (Strings [] args) {
int x = 3/0;
}
}
5.5
One would not need to declare that a method might throw an
IndexOutOfBoundsException,
because with some solid programming practices one should be
able to resolve this exception, which implies that the code is trying to access something that is
not there.
For example, it would be like, declaring an ArrayList
or List
, not initializing the array,
therefore leaving it empty, with no elements for the code to access. Java would throw this
exception to let the programmer know that they declared something, that it doesn’t have or that
is not defined.
5.6
The type of the exception object that gets thrown is always the same as the exception
type declared in the catch clause that catches an exception. Essentially, the program will search
for the block of code that handles that exception, if the handler is not found, then it cancels.
Haley Goodwiler
1216356739
hgoodwil
Homework 1
Exceptions and Exception Handling
6.3
A) In a static method, it is easy to differentiate between class to instance methods and
calls to static methods. One can tell the difference by the syntax. For example, in Java an
automatic instance variable is this
, in code it would be:
this.a = a;
this.b = b;
Static methods are not going to change, will remain the same despite other factors.
These calls also tell the compiler that the variable is a class variable, not an instance variable,
and these variables belong to, and are called on by, the class. For example in code:
class Student31{
int age;
String name;
static String college = "Arizona State University";
static void change(){
college = "ASU"; }
}
B) An instance method applies to an instance of the class (i.e. an object) using
parameters whereas a static method can have no defined parameters. Our most frequent
example of a class method is the Main
required in every Java application,
public class Main (){
public static void main (Strings [] args){
}
}
Example of a static method:
static int Math.round (double x)
6.8(b,c,f,h,k,m)
b) int a2 = H01_65.mY
This statement did not compile. When this statement was executed, a pop-up error
message notified me that,
“One or more projects were compiled with errors. Application you are running may end
unexpectedly. “
c) int a3 = H01_65.A
This statement did compile because the int declared as a3 was set to equal a
class.object that have been declared and defined within the code.
f) int a5 = cObj1.getX();
This statement did compile because the int declared as a5 is set to equal a
class.object that have been declared and defined within the code.
h) cObj2.setX(cObj1.getX());
This statement did compile. The same object type cObj2.setX passes an object of
the same type cObj1.getX().
k) int a7 = cObj1.getY();
This statement did compile because the int declared as a7 is set to equal a
class.object that have been declared and defined within the code.
m) int a8 = H01_65.getY();
This statement did not compile. This statement is declaring a non-static method get()
that cannot be referenced from a static context. When the program ran, the message given was,
“Incompatible source code. Exception in thread “main” java.lang.RuntimeException.”
7.2
a) Course has one instance of the Roster class and Roster does not have any instance of
the Course class.
b) Roster has 0 to n instance of the Student class and Student is not having any instance of
the Roster class.
c) No relation is between the Student and the UndergradStudent because neither has
instances of each other.
d) No relation is between the Student and the GradStudent because neither has instances
of each other defined.
e) No relation is between the GradStudent and the UndergradStudent because neither has
instances of each other.