Java hands-on brain-exception handling

Source: Internet
Author: User

Java07 Abnormal handling hands-on brain

    1. Basic knowledge of exception handling

Java exception handling is managed by 5 keyword try, catch, throw, throws, finally. The basic process is to wrap the statement that you want to monitor with a try statement block, and if an exception occurs within a try statement block, the exception is thrown, your code can catch the exception in the Catch statement block, and the exception that is generated by some system is automatically thrown in the Java runtime. You can also declare the method to throw an exception by using the throws keyword on the method, and then throw the exception object through a throw inside the method. The finally statement block executes before the method executes a return, and the general structure is as follows:
try{
Program code
}catch (Exception type 1 exception variable name 1) {
Program code
}catch (Exception type 2 exception variable name 2) {
Program code
}finally{
Program code
}

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

}

}

}

Operation Result: arrayindexoutofboundsexception/inner layer Try-catch

occur ArithmeticException

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

}

}

}

Running result: arrayindexoutofboundsexception/outer try-catch

3. read the Embedfinally.java example, run it, observe its output, and summarize it.

Multiple catch blocks, the Java Virtual Opportunity matches one of the exception classes or its subclasses, executes the catch block, and no other catch block is executed. The finally statement must execute code in any case, which guarantees that some code must be performed reliably in any situation.

4. is the finally statement block bound to execute?

Not necessarily, if the program is terminated prematurely, the finally statement will not be executed, such as System.exit (0) in the program, the early end of the program.

5. 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.

importjava.util.*;

class aexception extends Exception

{

Stringa;

Aexception ()

{

A= "Incorrect input";

}

Public String toString ()

{

return A;

}

}

Public class A

{

Public Static void Main (String args[])

{

while (1>0)

{

Scanner sc = new Scanner (System. in);

System. out. println ("Please enter exam results (0~100):");

Try

{

String s = sc.nextline ();

Getnum (s);

}

Catch (Aexception e)

{

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

}

}

}

Private Static void Getnum (String s) throws Aexception

{

for (int i = s.length ()-1; I >= 0;i--)

{

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 grid \ n");

}

Else if (Num >= && num <= 70)

{

System. out. Print ("Pass \ n");

}

Else if (num>= && num<= 80)

{

System. out. Print ("Medium \ n");

}

Else if (Num >= && num <= 90)

{

System. out. Print ("Good \ n");

}

Else

{

System. out. Print ("excellent \ n");

}

}

}

Java hands-on brain-exception handling

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.