JAVA polymorphism and exception handling jobs--hands-on brain and experimental problems after class

Source: Internet
Author: User
Tags getmessage

1, read the following code (Catchwho.java), write out the program running results:

1) Source Code

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 ("occurs ArithmeticException");

}

Catch (ArrayIndexOutOfBoundsException e)

{

System. out. println ("arrayindexoutofboundsexception" + "/outer try-catch");

}

}

}

2) Running results

1, write out the results of Catchwho2.java program operation

1, 1) source code

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 ("occurs ArithmeticException");

}

Catch (ArrayIndexOutOfBoundsException e)

{

System. out. println ("arrayindexoutofboundsexception" + "/outer try-catch");

}

}

}

1) Running results

3. Please read the Embedfinally.java example before running it, observe its output and summarize it.

1) Source Code

Public class Embededfinally

{

Public Static void Main (String args[])

{

int result;

Try

{

System. out. println ("at level 1");

Try

{

System.  out. println ("at Level 2");//result=100/0; Level 2

Try

{

System. out. println ("at Level 3");

result=100/0; Level 3

}

Catch (Exception e)

{

System. out. println ("Level 3:" + E.getclass (). toString ());

}

finally

{

System. out. println ("at level 3 finally");

}

result=100/0; Level 2

}

Catch (Exception e)

{

System. out. println ("Level 2:" + E.getclass (). toString ());

}

finally

{

System. out. println ("at level 2 finally");

}

result = 100/0; Level 1

}

Catch (Exception e)

{

System. out. println ("Level 1:" + e.getclass (). toString ());

}

finally

{

System. out. println ("at level 1 finally");

}

}

}

2) Running results

1) Summary

Executes a try statement, has catch execution, and finally executes a finally statement.

4, discrimination: Finally statement block will be executed? Please answer these questions through the Systemexitandfinally.java sample program

1) Source Code

Public class systemexitandfinally {

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");

}

}

}

2) Running results

3) Summary

The finally statement is not necessarily executed, and in the Catch statement, an exit (0) statement exits. To see the specifics, the catch statement is handled.

5, write a program, this program at run time requires the user input an integer, representing a course of the exam results, the program then give "failed", "Pass", "Medium", "good", "excellent" conclusion. Requires that the program be robust enough that it does not crash regardless of what the user enters.

1) Source Code

import java.io.*;

Public class Grade {

Public Static void Main (string[] args) {

TODO auto-generated Method stub

String a = null;

int g;

BufferedReader Strin =new bufferedreader (new InputStreamReader (System. in));

Try

{

System. out. println ("Please enter a score (integer):");

A = Strin.readline ();

}

Catch (Exception e)

{

System. out. println (E.getmessage ());

}

Try

{

g = Integer. parseint (a);

if (g<=100&&g>=90)

System. out. println ("excellent");

Else if (g>=80&&g<90)

System. out. println ("good");

Else if (g>=70&&g<80)

System. out. println ("medium");

Else if (g>=60&&g<70)

System. out. println ("Pass");

Else if (g<60)

System. out. println ("failing");

Else

System. out. println ("score error");

}

Catch (NumberFormatException e)

{

System. out. println ("input error");

}

}

}

2) Running results

JAVA polymorphism and exception handling jobs--hands-on brain and experimental problems after class

Related Article

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.