Java Exception handling mechanism

Source: Internet
Author: User
Tags throw exception

/*
* Exception handling mechanism
* 1. Category: Error and exception
* Error errors are the JVM automatically error, the programmer can not solve such as open array too large int a[]=new int [1024*1024*1024];
* Exception error when programmer to solve problems, such as pointer out of bounds, zero to do divisor
* 2. Exception handling
* try{
* Code to detect if an exception occurred
* }
* catch (Exception e) {
* Code that handles exceptions (typically printing error messages)
* }
* finally{
* Code that must be run (typically, operations that release resources such as shutting down the database)
* }
* */

1  Public classTest {2      Public Static voidMain (string[] args) {3         intA=10,b=0,c=0;4         Try{5C=div (A, b);//code that could be faulted, put in try to detect6}Catch(Exception e) {7             //your own definition of output content8System.out.println ("The divisor is 0!") ");9             //get the error messageTen System.out.println (E.getmessage ()); One             //exception name and exception information A System.out.println (e.tostring ()); -             //Exception name + exception information + exception location (JVM default exception handling mechanism) - e.printstacktrace (); the}finally{ -SYSTEM.OUT.PRINTLN ("The program is wrong please check"); -         } -          +System.out.println ("After Operation C=" +c); -     } +     //It is necessary to add throws exception after the function name that is likely to throw an exception to declare that it may throw an exception and improve the security of the program. A     //The method must be called where the Try-catch statement is required, otherwise the compilation error at      Public Static intDiv (intAintbthrowsexception{ -         returnA/b; -     } -}

/*
* The code blocks in try and finally are sure to be executed
* The code block in catch only executes if there is an exception (if there are multiple catch, only one is executed, and only one exception is thrown)
*
*
* Of course we will use some custom exceptions, let's introduce the contents of the custom exception
*
* Custom exception must inherit exception class, no person cannot throw exception */

1  Public classTest {2      Public Static voidMain (string[] args) {3         intA=10,b=-9,c=0;4         Try{5C=div (A, b);//code that could be faulted, put in try to detect6}Catch(fushuexception e) {7             //your own definition of output content8System.out.println ("Divisor is negative!") ");9             //Call the ToString method, and the GetValue method, to print the appropriate Error statementTenSystem.out.println (e.tostring () + "negative value is:" +E.getvalue ()); One}finally{ ASYSTEM.OUT.PRINTLN ("The program is wrong please check"); -         } -          theSystem.out.println ("After Operation C=" +c); -     } -     //It is necessary to add throws exception after the function name that is likely to throw an exception to declare that it may throw an exception and improve the security of the program. -     //The method must be called where the Try-catch statement is required, otherwise the compilation error +      Public Static intDiv (intAintbthrowsfushuexception{ -         if(b<0){ +             //because it is the exception class that is written by the individual, you create the exception class yourself and throw A             Throw NewFushuexception ("input divisor is negative---/by negative number", b); at         } -         returnA/b; -     } - } - /* - * defines a negative exception class that throws an exception if the divisor is negative in  * */ - classFushuexceptionextendsexception{ to     Private intvalue; +      PublicFushuexception (String msg,intvalue) { -         //there are assignment functions and output functions in the parent class of exception, so only the constructor of the parent class needs to be called the         Super(msg); *          This. value=value; $     }Panax Notoginseng      Public intGetValue () { -         returnvalue; the     } +      A}

/*
* The difference between throws and throw
* 1. Different locations
* Throws is placed behind the function name
* Throw in function
* 2. The following content is different
* throws followed by one or more exception class names, separated by commas
* Throw behind with an object
* */

Java Exception handling mechanism

Related Article

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.