Need some help of thoughts on my final project extra cridet. Any help are welcom
ID: 3840678 • Letter: N
Question
Need some help of thoughts on my final project extra cridet. Any help are welcomed! thank you!
Question 1: Explain Exception Handling
What are the rules for what must be included with a try-block (e.g., catch, finally, etc.)?
What is the difference between a checked exception and an unchecked exception?
What is the difference between the keyword throws and throw? (This is a classic interview question.)
Question 2: Tracing Exception Handling
Use the pseudocode below. Which statements will be executed if:
no exceptions are thrown
an ExceptionA is thrown in statement8
an ExceptionB is thrown in statement8
an ExceptionC is thrown in statement8
an ExceptionD is thrown in statement8
public void methodA() {
statement1
try {
methodB();
statement2
} catch(ExceptionB ex) {
statement3
} catch(ExceptionC ex) {
statement4
} finally {
statement 5
}
statement6
}
public void methodB() {
statement7
try {
statement8 // this statement generates the exception when one is thrown
statement9
} catch(ExceptionA ex) {
statement10
} catch(ExceptionB ex) {
statement11
} finally {
statement12
}
statement13
}
Explanation / Answer
This example shows how to print stack of the exception using printStack() method of the exception class.
public class Main{
public static void main (String args[]) {
int array[] = {20,20,40};
int num1 = 15, num2 = 10;
int result = 10;
try {
result = num1/num2;
System.out.println("The result is" +result);
for(int i = 5; i >=0; i--) {
System.out.println("The value of array is" +array[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Result
The above code sample will produce the following result.
The result is1
java.lang.ArrayIndexOutOfBoundsException: 5
at Main.main(Main.java:11)
The following is an another example of print stack of the Exception in Java.
public class Demo {
public static void main(String[] args) {
try {
ExceptionFunc();
} catch(Throwable e) {
e.printStackTrace();
}
}
public static void ExceptionFunc() throws Throwable {
Throwable t = new Throwable("This is new Exception in Java...");
StackTraceElement[] trace = new StackTraceElement[] {
new StackTraceElement("ClassName","methodName","fileName",5)
};
t.setStackTrace(trace);
throw t;
}
}
The above code sample will produce the following result.
java.lang.Throwable: This is new Exception in Java...
at ClassName.methodName(fileName:5)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.