Hands-on brain: multilayer anomaly capture -1
Read the following code (Catchwho.java) to write out the results of the program run:
public class Catchwho {
public static void Main (string[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException ();
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("arrayindexoutofboundsexception" + "/ inner layer try-catch");
}
throw new ArithmeticException ();
}
catch (ArithmeticException e) {
System.out.println (" occurring arithmeticexception");
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("arrayindexoutofboundsexception" + "/ outer try-catch");
}
}
}
Program Results :
Hands-on brain: multilayer anomaly capture -2
Write The results of the Catchwho2.java program run
public class CatchWho2 {
public static void Main (string[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException ();
}
catch (ArithmeticException e) {
System.out.println ("arrayindexoutofboundsexception" + "/ inner layer try-catch");
}
throw new ArithmeticException ();
}
catch (ArithmeticException e) {
System.out.println (" occurring arithmeticexception");
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("arrayindexoutofboundsexception" + "/ outer try-catch");
}
}
}
Program results:
Analysis: Thefinally statement block will be executed?
Please answer these questions through the systemexitandfinally.java sample Program
public class Test {
public static void Main (string[] args)
{
try{
SYSTEM.OUT.PRINTLN ("in Main");
throw new Exception ("Exception is thrown in main");
System.exit (0);
}
catch (Exception e)
{
System.out.println (E.getmessage ());
System.exit (0);
}
Finally
{
System.out.println ("in finally");
}
}
}
Program results:
Write a program that requires the user to enter an integer at run time, representing the exam results of a course, followed by a "fail", "Pass", "Medium", "good", "excellent" conclusion.
Requires that the program be robust enough that it does not crash regardless of what the user enters.
SOURCE program code:
Import Java.util.Scanner;
Class Aexception extends Exception
{
String error;
Aexception ()
{
Error= " Please note that the requirement is The number of the input 0~100 ";
}
Public String GetError ()
{
return error;
}
}
Class Test
{
public static void Main (String args[])
{
Scanner sc = new Scanner (system.in);
System.out.println (" Please enter exam results (0~100):");
Try
{
String s = sc.nextline (); Type of read-in screen converted to type S
Getnum (s);// Determine if an exception is thrown
}
catch (Aexception e)
{
System.out.println (E.geterror ());
}
}
private static void Getnum (String s) throws Aexception
{
for (int i = s.length ();-I. >= 0;)
{
int chr = S.charat (i);
if (Chr < | | CHR > 57)
{
throw new Aexception ();
}
}
Double num = double.parsedouble (s);
if (num < 0 | | num> 100)
{
throw new Aexception ();
}
if (num>= 0 && num<= 60)
{
System.out.print (" less than lattice ");
}
else if (num >= && num <= 70)
{
System.out.print (" pass ");
}
else if (num>= && num<= 80)
{
System.out.print (" medium ");
}
else if (num >= && num <= 90)
{
System.out.print (" Liang ");
}
Else
{
System.out.print (" excellent ");
}
}
}
Question Answer:
The finally statement block executes only if the TRY statement block corresponding to finally is executed. If a try statement block is returned (return) or an exception is thrown, the finally statement block of the try will not execute
Polymorphism and exception handling after-class operations