Java Exception Handling

Source: Internet
Author: User

1. Abnormal

An exception is a flow of instruction that causes the program to run, and if the exception is not handled correctly, it can cause the program to break execution and cause unnecessary loss.

2. Handling Exceptions

try{

Exception statement

}catch (Exception e) {

}finally{

Code that is bound to execute

}

If you know the type of the exception can be written directly in the catch, for example: ArithmeticException E

You can capture a multiple catch statement after the try.

3. Common anomalies

A, nullpointerexception null pointer exception

B, arithmeticexception arithmetic anomalies

C, arrayindexoutofboundsexception array out of bounds exception

D, numberformatexception numeric format exception

4. Throws key Words

A, when defining a method, you can use the throws keyword declaration, using the method declared by throws to indicate that this method does not handle the exception, and throws it to the caller of the method.

The format is:

public void Tell () throws exception{}

Example:

If an exception occurs, the function that calls the method catches the exception with the corresponding try catch operation.

If there is no action to catch the exception, an error may occur.

5. Throw keyword

The Throw keyword throws an exception that directly throws an instantiated object of the exception class.

  

1         Try {2             Throw New Exception ("Instantiate exception object"); 3         } Catch (Exception e) {4            System.out.println (e); 5         }6     }

6. Custom Exceptions

A, custom exception directly inherit exception can complete the custom exception class

    

1 classMyExceptionextendsexception{2       Publicmyexception (String msg) {3         4         Super(msg);//constructor method for calling exception5     }6 }7  Public classHello {8 9      Public Static voidMain (string[] args) {Ten              One         Try{ A             Throw NewMyException ("Custom Exception"); -}Catch(MyException e) { - System.out.println (e); the         } -     } -}

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