Java Exception Handling (1)

Source: Internet
Author: User

1. Difference between error and exception

2. Difference Between exception and runtimeexception

3. Try {...} catch (...) {...} finally {...} execution order

 

Public Class X {

Public static void main (string [] ARGs ){

 

// Error and exception have the same parent class throwable

// "Error is a subclass of throwable. Its appearance indicates a serious problem. General applicationsProgramNo error should be caught unless there is a reason. This is usually a very unusual situation"

// Although error can be captured, programmers are generally not recommended to do this.

Throwerror (); // No processing is performed.

Try {

Throwerror ();

} Catch (error ex) {// to capture, it must be an error and cannot be exception

System. Out. Print ("B ");

} Finally {

System. Out. Print ("C ");

}

// Exception is also a subclass of throwable. It is divided into runtime exceptions and checked exceptions by checked | unchecked. Both exceptions are subclasses of exception.

// Checked exception, which must be handled by the programmer. It must be either thrown to the previous layer (declared in the throws Statement defined by the method) or handled by try {} catch () {}.

// If no exception is processed, the compilation fails.

// Throwexception (); // compilation Error

Try {

Throwexception ();

} Catch (exception e ){

//

} Finally {

//

}

// Runtime exception runtimeexception is unchecked, common except 0 exception, null pointer exception

Throwruntimeexception ();

}

Public static void throwerror (){

Throw new error ();

}

Public static void throwexception () throws exception {

Throw new exception ();

}

Public static void throwruntimeexception (){

Throw new runtimeexception ();

}

}

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.