Java (17) exception

Source: Internet
Author: User

I. Exceptions

1.1 During the execution of the program, the effect of the program is running normally.

1.2 Exception Syntax

try{

code block

}catch (Exception type E) {

}catch (Exception type E) {

}... {

}finally{

Note:try: A block of code that represents an exception that might occur

Catch: Grab exceptions and handle them

May crawl multiple exceptions, the range of exceptions to small to large crawl

And only the first matching exception type is executed

Finally: The last, regardless of whether or not an exception occurs, the code in the finally will be executed unless the virtual machine (Syatem.exit (1) is outside the case)

Use the IF structure:

1  Public classText1 {2     /*3 * 1. What is an exception? Definition: An exception is an event that occurs when a program executes, and it interrupts the normal flow of instructions. 4      */5      Public Static voidMain (string[] args) {6Scanner input =NewScanner (system.in);7System.out.println ("Please enter dividend");8         intnum = 0;9         if(Input.hasnextint ()) {Tennum =input.nextint (); One}Else{ ASYSTEM.OUT.PRINTLN ("Input dividend is not an integer, program exits"); -System.exit (1); -         } theSystem.out.println ("Please enter divisor"); -         intNUM1 = 0; -         if(Input.hasnextint ()) { -NUM1 =input.nextint (); +             if(0==NUM1) { -SYSTEM.OUT.PRINTLN ("The input divisor is 0, the program exits"); +System.exit (1); A             } at}Else{ -SYSTEM.OUT.PRINTLN ("The input divisor is not an integer, the program exits"); -System.exit (1); -         } -System.out.println (String.Format ("%d/%d =%d", num,num1,num/num1)); -SYSTEM.OUT.PRINTLN ("Thank you for using this program"); in  -     } to}
Example 1

The result of the operation is:

Using try-catch-finally:

1 //handling exceptions using try Catch finally2  Public classTest2 {3      Public Static voidMain (string[] args) {4Scanner input =NewScanner (system.in);5System.out.println ("Please enter dividend");6         Try {7             intnum =input.nextint ();8System.out.println ("Please enter divisor");9             intNUM1 =input.nextint ();TenSystem.out.println (String.Format ("%d/%d =%d", num, NUM1, num/num1)); One}Catch(Exception e) { ASYSTEM.ERR.PRINTLN ("error occurred, divisor and divisor must be integers," + "divisor cannot be zero"); - System.out.println (E.getmessage ()); -             //err is the direction of the output stream for run-time exceptions and error feedback, but it can only be printed on the screen, even if you redirect the             //Redirection is a way to redirect various network requests in different directions to another location -             //the string that is printed with err • The eclipse input will be displayed in red; -}finally { -System.out.println ("Thanks for using this program! "); +         } -     } + } A /* at * If all statements in the try block are executed properly, then the finally block executes - * If the TRY statement block encounters an exception during execution, the code in the finally block is executed regardless of whether the exception can be caught by the catch block - * Note: Even if a return statement exists in a try block and a catch block, the statement in the finally block executes - * Order of execution When an exception occurs: Executes a statement before a return in a try block or catch, executes a statement in a finally block, executes a return statement in a try or catch exit - * The only case in which a statement in a finally block is not executed: executing system.exit (1) in the exception handling code will exit the Java Virtual machine -  */
Example 2

The result of the operation is:

1.3 Throws Declaration exception

A. Is the current method, cannot resolve this exception, you must give this exception to the last call to handle

B. Syntax

Access modifier return value type method name (argument list) [throws Exception]{}

two. Java Exception System

Throwable

|--error: (Errors that cannot be handled by the program)

|--exception: Errors that can be crawled or thrown with the program

|--Check for exceptions (non-runtime Exceptions): Exceptions that occur during compilation

Sqlexceqtion,

IOException,

ClassNotFoundException

|--Non-check exception (run-time exception runtimeexception): Run simple exception that occurs

NullPointerException,

ArrayIndexOutOfBoundsException,

ClassCastException

Note:Cgecked exception, is required to process

Run-time exception, can not be handled

Java (17) exception

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.