Java basics --- Exception Handling, java basics Exception Handling

Source: Internet
Author: User

Java basics --- Exception Handling, java basics Exception Handling

Recognition exception

An instruction stream that causes program interruption when an exception occurs. If the exception is not correctly handled, the program may be interrupted and cause unnecessary losses, therefore, in the program design, we must consider the occurrence of various exceptions and properly handle them so as to ensure the normal execution of the program, in java, an error capture mechanism called exception handling is used for processing.

To handle exceptions in a program, you must study possible errors and problems in the program.

  • User input error ()
  • Device Error
  • Physical restrictions (when the disk is full ,)
  • Code error (Program methods may fail to be executed normally)

Exception Handling

Simple format for Exception Handling

If any code in the try statement block throws an exception class described in the catch clause

1) The program will skip the remaining code of the try statement block.

2) The program will execute the processor code in the catch clause.

If the code in the try statement block does not throw any exception, the program skips the catch clause.

At the same time, you can capture multiple exceptions in a try statement block and handle different exceptions differently.

When the code throws an exception, it will terminate the processing of the remaining code in the method, exit the execution of this method, and then execute the statements in the finally clause.

Inheritance result of the exception class

There are two common classes in the Exception results of java: Exception and Error, both of which are subclasses of Throwable.

  • Exception: one side indicates a problem in the program. You can use try... catch to handle it directly.
  • Error: generally a JVM Error, which cannot be processed in the program

All the above mentioned are subclasses of Exception.

Java Exception Handling Mechanism

Instance 1

Handle package exceptions; public class ExceptionDemo01 {public static void main (String args []) {System. out. println ("*********** computing start ************"); int I = 0; // define the integer variable int j = 0; // define the integer variable try {String str1 = args [0]; // receive the first parameter String str2 = args [1]; // receives the second parameter I = Integer. parseInt (str1); // convert the first parameter from string to Integer j = Integer. parseInt (str2); // Changes the second parameter from a string to an integer int temp = I/j; // an exception is reported here. out. println ("result of division of two numbers:" + temp); System. out. println ("----------------------------");} catch (ArithmeticException e) {// catch an arithmetic exception // System. out. println ("arithmetic exception:" + e); e. printStackTrace ();} catch (NumberFormatException e) {// catch the number conversion exception System. out. println ("number conversion exception:" + e);} catch (ArrayIndexOutOfBoundsException e) {// capture array out-of-bounds exception System. out. println ("array out-of-bounds Exception:" + e);} catch (Exception e) {System. out. println ("Other exceptions:" + e);} System. out. println ("*********** computing end ***********");}};

Print results

* ******************** Array out-of-bounds exception: java. lang. arrayIndexOutOfBoundsException: 0 ************ computing end ***********

 

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.