Java Classroom Exercises--exceptions

Source: Internet
Author: User
Tags try catch

1. Read and run the Aboutexception.java sample, and then follow the next few pages to learn the basics of implementing exception handling in Java.

No exception occurs when code executes.

When processing floating-point numbers, the JVM generates DDIV bytecode instructions, i/0,0 into floating-point number 0.0, and 0.0 is double, not accurate, so no exception is thrown.

When the JVM processes integers, it generates IDIV bytecode instructions, and integers except 0 will throw exceptions except 0.

The first program did not execute finally?

Finally must be used in conjunction with a try or try/catch. Because the try is not executed, finally is not executed, and the exception is captured by the system, so the try is not thrown. In addition, you cannot exit the try without executing its finally. If finally exists, it is always executed. (from that point of view, the statement is correct.) There is a way to exit a try without performing a finally. If the code executes a system.exit (0) inside a try; Statement, then the application terminates without performing a finally execution. On the other hand, finally will not execute if you unplug the power during try execution. )

2. Read the following code (Catchwho.java) to write out the results of the program operation:

3. write The results of Catchwho2.java program operation

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

Please pass Systemexitandfinally.java Sample Program answers the above questions

Finally is not executed in this program because of the system.exit(0) in the catch statement ; causes the program to exit JVM up, so finally will not be carried out.

summarizes the try catch finally nesting using the execution order.

1)The Try statement is nested from the outer to the inner layer execution, in the try statement, which layer error, which layer throws an exception, the back of the try statement is no longer executed, if the layer exists the catch is captured accordingly , and finally the layer is executed, unless the finally encounters a condition that does not execute;

2) If the layer has no catch to capture, it throws out, to find catch, if there is no catch to capture, Terminates the program.

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

1) after a try throws an exception, the program jumps out of the try, no longer executes The statement behind the try, and starts the catch make a match and handle the exception;

2) in a try nest, the exception that is thrown is only processed to throw the next exception in order, and if it is not processed, the program terminates ;

3) After the try throws an exception, it jumps out of the try statement, the inner catch cannot be caught and then continues to throw out, so the outer layer has an exception, the outer statement does not execute, the second program throw new Arithmeticexcepption not executed.

4) The third program, try the first layer of the second layer no exception is not captured, after execution to the third layer, except 0 has an exception,catch capture, perform the third layer of finally then, sequentially executes the second layer, the first layer of the finally .

6. 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:

import Java.util.Scanner;

class cepingcuowuexception extends exception{

Public cepingcuowuexception (String s) {

Super (s);

}

}

class fenshu{

int score;

Public void Panduan (int score) {

score = score;

if (score<60)

{

System. out. println ("Less than a lattice!" ");

}

if (score>=60) {

{

System. out. println ("Pass! ");

if (score<=70)

System. out. Print ("medium");

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

System. out. Print ("good");

Else if (score>=80)

System. out. Print ("excellent");

}

}

}

}

Public class Test {

Public Static void Main (String args[])throws cepingcuowuexception{

Fenshu Fenshu = new Fenshu ();

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

Try {

Scanner in = new Scanner (System. in);

String B = In.next ();

boolean result=b.matches ("[0-9]+");

if (result==true)

{

int C=integer. valueOf (b);

if (C > 100| | C < 0)

Throw New cepingcuowuexception ("The input range is incorrect! ");

Else

Fenshu.panduan (c);

}

if (Result = = false)

Throw New cepingcuowuexception ("Incorrect input format!) ");

}

Catch (Cepingcuowuexception e) {

System. out. println (e);

}

}

}

Program:

Java Classroom Exercises--exceptions

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.