23.3k views
3 votes
Write a program that illustrates rethrowing an exception. Define methods CISP401Method and CISP401Method2. Method CISP401Method2 should initially throw an exception. Method CISP401Method should call CISP401Method2, catch the exception and rethrow it. Call CISP401Method from method main, and catch the rethrown exception. Print the stack trace of this exception.

1 Answer

2 votes

Answer:

See explaination

Step-by-step explanation:

The code here:

import java.io.*;

public class CISP401V11A5 {

public static void main(String[] args) throws Exception {

try {

CISP401Method();

} catch (Exception e) {

System.out.println("Exception thrown in " + e.getStackTrace()[0].getMethodName() + "\\");

StringWriter sw = new StringWriter();

e.printStackTrace(new PrintWriter(sw));

String exceptionAsString = sw.toString();

String bagBegin = exceptionAsString.substring(0, 19);

String bagEnd = exceptionAsString.substring(19);

System.out.println(bagBegin + ": Exception thrown in " + e.getStackTrace()[0].getMethodName() + bagEnd);

}

}

public static void CISP401Method() throws Exception {

try {

CISP401Method2();

} catch (Exception e) {

throw e;

}

}

public static void CISP401Method2() throws Exception {

throw new Exception();

}

}

See attachment for sample output

Write a program that illustrates rethrowing an exception. Define methods CISP401Method-example-1
User AnOldSoul
by
4.7k points