exception handling Mechanisms in Java

Source: Internet
Author: User

exception handling Mechanisms in Java

Example

static void main(String[] args) {// TODO Auto-generated method stubString s="hello";int i=Integer.parseInt(s);}}

Run Exception results

In the above code, integer.parseint means to convert the string type to an integer type, while the integer is an int wrapper class, the program will error, because the computer has no way to convert hello to an integer, then you can use the following ways to solve the problem

public  Class test {/** * Try block indicates something that may be wrong * The CATCH block is the part that executes after the error * */public static void main (String[] args) {//TODO auto-generated method stubstring s=try {int i=integer.parseint (s); catch (NumberFormatException e) {/** * * * program does not go wrong when it is not executed in the CATCH block //TODO auto-generated catch Blocke.printstacktrace ();}}      

You can also do this in a catch block: System.out.println (e.getmessage)----Print out Error statement information;; E.printstacktrace () and print stack information; The events in the above example can also be handled or not handled

Errors that must be handled

public static void main(String[] args){ Class.forName("java.test");}

The error thrown is: Unhandle Exception type classnotfoundexception--> class did not find an exception error, this error system requirements must be handled, processing the same way as try{}catch{} to handle

Exception handling mechanism: An error that occurs when a program is running, which causes the program to not function correctly is called an exception

    1. Error: General JVM run errors, no processing, no way to handle
    2. Exception is divided into runtimeexception and non-runtimeexception, run exception can choose to handle or not to handle, non-running exception can not be too much

Summary of Exception types

RuntimeException:

        1. nullfointerexcption--> NULL pointer exception
        2. Indexoutofboundexception--> array subscript out of bounds exception
        3. Numberformatexception--> Data Format exception
        4. Classcastexception--> Type Conversion exception
        5. Illegalargumentexception--> Illegal parameter exception
        6. Arithmeticexception--> Arithmetic exception

Non-runtimeexception:

        1. Classnotfoundexception--> Class Load exception

exception handling Mechanisms in Java

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.