Java polymorphism and exception handling after-class jobs

Source: Internet
Author: User
Tags getmessage

1. Hands-on brain: Read and run the Aboutexception.java sample and then learn the basics of implementing exception handling in Java through the next few pages of PPT

Import javax.swing.*;

Class Aboutexception {

public static void Main (string[] a)

{

int I=1, j=0, K;

k=i/j;

Try

{

K = i/j; Causes Division-by-zero exception

throw new Exception ("hello.exception!"); }

catch (ArithmeticException e)

{

System.out.println ("by 0" divide. "+ e.getmessage ());

}

catch (Exception e)

{

if (e instanceof arithmeticexception)

System.out.println ("by 0 divide");

else {

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

}

}

Finally
{
Joptionpane.showconfirmdialog (null, "OK");
}

}
}

The code in the Catch statement block is used to handle errors. When an exception occurs, the program control process jumps from the TRY statement block to the catch statement block. The statements in the finally statement block are always guaranteed to be executed, regardless of whether an exception occurs.

There can be more than one catch statement block, and each block of code captures an exception. It is a syntax error to have two different catch blocks after a try block to catch two of the same type of exception.

With a catch statement, you can only capture objects of the exception class and its subclasses. Therefore, a catch statement block that captures the exception object captures all the "catch" exceptions.

Placing catch (Exception e) in front of other catch blocks causes these catch blocks to not execute, so Java does not compile the program.

2. Hands-on brain: multi-layered anomaly capture-1, read the following code (Catchwho.java), write out the program running results:

Operation Result:

arrayindexoutofboundsexception/Inner Layer Try-catch

occur ArithmeticException

3. Hands-on brain: multi-layered anomaly capture-2, write the results of Catchwho2.java program run

arrayindexoutofboundsexception/outer Try-catch

4. Please read the Embedfinally.java example before running it, observing its output and summarizing it.

The operation results are as follows

When there are multiple layers of nested Finally, exceptions are thrown at different levels and thrown at different locations, which can result in a different order of the finally statement block execution.

5. Discrimination: The finally statement block will certainly be executed?

Please answer these questions through the Systemexitandfinally.java sample program

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

}

}

}

The finally statement block is bound to execute

6. Hands-on Brain, write a program, this program at run time requires the user input an integer, representing a course of 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.

Import Javax.swing.JOptionPane;

public class test{

public static void Main (string[] args) {

TODO auto-generated method stubs

Double n=0.0;

for (int i=0;i<1000000000;i++)

{

String input=joptionpane.showinputdialog ("Please enter student scores");

try{

N=double.valueof (input);

if (n<0)

{

Joptionpane.showmessagedialog (NULL, "incorrect input");

}

else if (n<60)

{

Joptionpane.showmessagedialog (NULL, "failure of the student to pass the Grade");

}

else if (n<70)

{

Joptionpane.showmessagedialog (NULL, "the student's grade is passed");

}

else if (n<80)

{

Joptionpane.showmessagedialog (NULL, "the student's grade is Medium");

}

else if (n<90)

{

Joptionpane.showmessagedialog (NULL, "the student's performance is Good");

}

else if (n<=100)

{

Joptionpane.showmessagedialog (NULL, "the student's performance is excellent");

}

else if (n>100)

{

Joptionpane.showmessagedialog (NULL, "incorrect input");

}

}

catch (NumberFormatException e)//numberformatexception exception

{

Joptionpane.showmessagedialog (NULL, "incorrect input");

}

}

}

}

Java polymorphism and exception handling after-class jobs

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.