Polymorphism and exception handling after-class operations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.