Using Java Create a custom exception class that overrides the getMessage( ) meth
ID: 3700943 • Letter: U
Question
Using JavaCreate a custom exception class that overrides the getMessage( ) method Create a class that manually throws an exception of your custom type Use System.out.println( ) to note where you are in the control flow. Example: “Starting try block”, “Ending try block”, “Starting catch block”, etc. Duck the exception at least once Implement a finally block that prints a graceful goodbye message Use the System.exit(0) command in the try block and rerun the application. Note the console output to see if the finally block executes. Using Java
Create a custom exception class that overrides the getMessage( ) method Create a class that manually throws an exception of your custom type Use System.out.println( ) to note where you are in the control flow. Example: “Starting try block”, “Ending try block”, “Starting catch block”, etc. Duck the exception at least once Implement a finally block that prints a graceful goodbye message Use the System.exit(0) command in the try block and rerun the application. Note the console output to see if the finally block executes.
Create a custom exception class that overrides the getMessage( ) method Create a class that manually throws an exception of your custom type Use System.out.println( ) to note where you are in the control flow. Example: “Starting try block”, “Ending try block”, “Starting catch block”, etc. Duck the exception at least once Implement a finally block that prints a graceful goodbye message Use the System.exit(0) command in the try block and rerun the application. Note the console output to see if the finally block executes. Create a custom exception class that overrides the getMessage( ) method Create a class that manually throws an exception of your custom type Use System.out.println( ) to note where you are in the control flow. Example: “Starting try block”, “Ending try block”, “Starting catch block”, etc. Duck the exception at least once Implement a finally block that prints a graceful goodbye message Use the System.exit(0) command in the try block and rerun the application. Note the console output to see if the finally block executes.
Explanation / Answer
//Implementation of try -catch block
//Code to copy
//Tester.java
public class Tester {
public static void main(String[] args) {
double numerator=10;
int denominator=0;
//try-catch block
try {
System.out.println("Starting try block");
//Check if denominator is 0
//then throws custom exception
if(denominator==0)
throw new CustomDivideByZero("Custome Exception : Cannot divide by 0");
//This will not run
double result=numerator/denominator;
System.out.println("result = "+result);
System.out.println("Ending try block");
} catch (CustomDivideByZero e) {
//catch block
System.out.println("Starting catch block");
System.out.println(e.getMessage());
System.out.println("Ending catch block");
}
finally
{
//finally block
System.out.println("finally ,good bye!.");
}
}
}//end of the class
-------------------------------------------------------------------
//CustomDivideByZero.java
public class CustomDivideByZero extends Exception{
//constructor
public CustomDivideByZero(String msg) {
super(msg);
}
//Override the getMessage method
public String getMessage() {
return super.getMessage();
}
}
-------------------------------------------------------------------
Sample Output:
Starting try block
Starting catch block
Custome Exception : Cannot divide by 0
Ending catch block
finally ,good bye!.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.