Java Exception Base Collation

Source: Internet
Author: User

1, Try-catch

E.printstacktrace (); Will print out the method's call traces, in this case, will print out the exception starts at the 16th line of testexception, so that it is easy to locate and analyze where the exception

2. Catch with the parent class of the exception class

FileNotFoundException is a subclass of exception, and using exception can catch FileNotFoundException

3, multi-exception capture Method 1

Catch (FileNotFoundException e) {    System. out. println ("D:/lol.exe does not exist ")    ; Catch (ParseException e) {    System. out. println (" date format parsing error ");    E.printstacktrace ();}

Multiple anomaly Capture Method 2: Another approach is to put multiple exceptions in a catch to uniformly capture

Catch (FileNotFoundException | parseexception e) {}
// This approach is supported from JDK7, and the benefit is that the captured code is more compact, and the disadvantage is that once an exception occurs, it is not possible to determine what kind of exception it is, and the specific type of exception needs to be judged by instanceof if (e instanceof filenotfoundexception) System. out. println ("D:/lol.exe does not exist "); if (e instanceof parseexception) System. out. println (" date format parsing error ");

4. Finally: The code in finally will be executed regardless of whether an exception occurs

5. Throw

Exception handling is required in METHOD2
But Method2 not to deal with it, but to throw it out through throws.
Then METHOD1 will receive the exception. There are two ways to deal with it, either a try catch or a throw away.
Method1 Select local Try Catch live once try catch, it is equivalent to digest this exception, the main method when calling Method1, do not need to do exception handling

Package exception; import java.io.file;import Java.io.fileinputstream;import java.io.FileNotFoundException; Public classTestException { Public Static voidMain (string[] args) {method1 (); }     Private Static voidmethod1 () {Try{method2 (); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); }     }     Private Static voidMethod2 () throws FileNotFoundException {File F=NewFile ("D:/lol.exe"); System. out. println ("trying to open D:/lol.exe"); NewFileInputStream (f); System. out. println ("successfully opened"); }}

Java Exception Base Collation

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.