Advanced Java Programming with Object-Oriented Programming Design VERY IMPORTANT
ID: 3711888 • Letter: A
Question
Advanced Java Programming with Object-Oriented Programming Design
VERY IMPORTANT NOTE, PLEASE READ BEFORE ANSWERING THE QUESTION BELOW!!: Because I'm in an advanced Java class that must use Object-Oriented Programming Design in all assignments, the answer for the question below MUST contain private variables, least 2 CLASSES, accessors and mutators for the private variables. You can use any other additional other object-oriented design (OOD) concepts if you want to but private variables, least 2 CLASSES, accessors and mutators for the private variables are a must! I've seen answers to the question below already posted, however they don't utilize object-oriented design (OOD) concepts such as a private variables, least 2 CLASSES, accessors and mutators for the private variables etc, which is required for this advanced Java programming class. So please answer the question meet the requirements for this question. Basically, I need advanced Java object-oriented design concepts for simple programs so here's to hoping this works out and if it does, I'll give a thumbs up, I promise!!!
Question:(Rethrowing Exceptions) Write a program that illustrates rethrowing an exception. Define methods someMethod and someMethod2. Method someMethod2 should initially throw an exception. Method someMethod should call someMethod2, catch the exception and rethrow it. Call someMethod from method main, and catch the rethrown exception. Print the stack trace of this exception.
Explanation / Answer
SomeClass.java
public class SomeClass{
private int a;
private String b;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public SomeClass(int a, String b) throws Exception{
if(a == 0 || b.equals(""))
throw new Exception("Null Exception");
this.a = a;
this.b = b;
}
@Override
public String toString() {
return "SomeClass [a=" + a + ", b=" + b + "]";
}
public void someMethod() throws Exception{
try {
this.someMethod2();
} catch (Exception e) {
System.out.println(e.toString());
System.out.println("Exception Handling in SomeMethod");
throw new Exception("Exception Call from SomeMethod");
}
}
private void someMethod2() throws Exception{
throw new Exception("Exception Call from SomeMethod2");
}
}
Driver.java
public class Driver {
public static void main(String[] args){
SomeClass sc = new SomeClass();
try {
sc.someMethod();
} catch (Exception e) {
System.out.println("Exception Handling in Main Program");
}
}
}
**Comment for any further queries.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.