Computer Programming 2
Exception 1
Week010 - Exception
Laboratory Exercise 009
Objective/s:
At the end of this activity, you should be able to:
· Demonstrate how Exceptions will be used
· Understand how to catch the exceptions
· Learn the advantage of exception handing in an application
What to Prepare for the Activity:
NetBeans IDE 8.2
JDK8 (Java Development Kit 8)
Procedure:
Create a NetBeans project for this activity. The project name should be as follows:
Project Name: Lab009_<lastname_firstname>
Example: Lab009_Blanco_Maria
Compress the NetBeans project into .rar or .zip format and then upload to the link
provided in the LMS.
Only NetBeans project compressed in .rar or .zip format will be accepted.
A. Given the following class:
public class MyClass {
public static void main(String[] args) {
int k=0;
try {
int i = 5/k;
} catch (Exception e) {
System.out.println("3");
} catch (ArithmeticException e) {
System.out.println("1");
} catch (RuntimeException e) {
System.out.println("2");
return;
Assessments
} finally {
System.out.println("4");
}
System.out.println("5");
}
}
Test Stem / Question Choices
1: Run the class above. What was the A: Compile Error
result?
B: Runtime exception
C: Arithmetic exception
D: run perfectly
2: What needs to be done to remove the A: Make the catch clause catching Exception as the last catch
error in the class? clause
B: Remove the finally clause
C: Set the value of k to 1.
D: Nothing
3: Now that you have fixed the code above, A: It printed:
what is the new result? 1
4
5
B: It printed:
3
1
5
C: It printed:
Computer Programming 2
Exception 3
1
2
5
D: It printed:
5
Since it is correct to begin with.
B. Given the following program, which statements are true?
public class ExceptionLabExercise {
public static void main(String[] args) {
try {
System.out.println(args[0]);
} finally {
Assessments
System.out.println("FINALLY");
}
}
}
C. Given the following classes,
public class TestClass {
public static void main(String[] args) throws A {
try {
f();
} finally {
System.out.println("Done.");
Test Stem / Question Choices
4: Run the class above. What A: It printed finally then thrown
happens when you run with no ArrayIndexOutOfBoundsException
arguments?
B: It printed ArrayIndexOutOfBoundsException
C: It printed finally
D: Nothing happend
5: If an A: Check the length of the array before printing the
ArrayIndexOutOfBoundsExceptio arguments
n occurred, what is the best way
to handle this? B: Add a catch clause for the said exception
C: Always use finally clause in order to avoid
exceptions
D: Nothing. That Exception needs not be caught.
} catch (Exception1 e) {
throw e;
Computer Programming 2
Exception 5
}
}
}
public class Exception1 extends Throwable{}
Test Stem / Question Choices
6: What is the problem with the A: The finally statement must be preceeded by the
above code? catch clause
B: Exceptions cannot be extended
C: It is illegal to throw an exception
D: Nothing is wrong
7: What should be done with the A: Move the catch clause before the finally clause.
code?
B: Do not use Throwable as the supertype of
Exception 1.
C: Do not throw the exception.
D: Nothing is wrong
8: What happens after you have A: “Done” is printed and an exception is shown in
been able to fix the code? the stack
B: “Done” is printed
C: Exception is shown in the stack
D: Nothing is wrong
Assessments
public class JavaApplication1 {
public static void main(String[] args) {
RuntimeException exception = null;
throw exception;
}
}
Test Stem / Question Choices
9: What is the problem with the A: null exception cannot be thrown
above code?
B: the main method must be declared with throws
statement
C: It is illegal to throw an exception
D: Nothing is wrong
10: How should you fix the code? A: A & B
Choose from the following:
A. Instantiate the exception B: A only
B. Add a throws statement in the
method declaration C: B only
C. Do no throw the exception
D: All is correct
Computer Programming 2
Exception 7
Assessments