The difference between Java exception and error

Source: Internet
Author: User
Tags throwable

The abstract class of exceptions in Java is Throwable, which is based on the derivation of two main classes: Error and exception.

Error is a serious error in the program and should not be included with Try...catch. Javadoc's instructions are as follows:

An Error was a subclass of Throwable that indicates serious problems, a reasonable application should not a try to catch. Most such errors is abnormal conditions.

Common error is: Assertionerror, OutOfMemoryError, Stackoverflowerror.

Exception is divided into two categories: Checked Exception and unchecked Exception.
The base class of unchecked Exception is runtimeexception, and all Exception inherited from runtimeexception belong to unchecked Exception, such as: NullPointerException, ArrayIndexOutOfBoundsException.
Checked Exception directly inherits from Exception, all Exception that do not inherit runtimeexception belong to Checked Exception, such as: FileNotFoundException.

Checked exception and unchecked exception can all be included with Try...catch. Where Checked exception checks for the desired catch handling at compile time, it is necessary to have try...catch included, or the corresponding exception is thrown at the method signature. And unchecked exception is judged at run time, so it doesn't have to be try...catch included. Moreover, unchecked exception is often a bug in the program itself, generally does not catch such errors.

Note 1:
Executing a return statement in a try will still execute a finally statement block unless the JVM process is ended in a try statement (System.exit (0))

public  class  hello  { public  static  void  main  (string[] args) {try  {System.out.println            ( "in Try" );        return ; }catch  (Exception e) {}finally  {syste        M.out.println ( "finally" );    } System.out.println ( "end" ); }}//output  in try   Finally   

Note 2:
do not call return in the Finally statement block, otherwise the exception thrown in the try statement block cannot be captured by the caller, as follows:

 try  {dosomething (); System.    out . println ( "No Exception caught!" ); } catch  (runtimeexception e) {system.. println (); }} public  static   void  dosomething  () {try  {throw  new  runtimeexception ();    } finally  {return ; }}//output  No Exception caught!  

Note 3:
The difference between throw and throws:
Throw is in the statement block, and the programmer actively throws an exception.
Throws is the signature of the method that describes the exception that the method might throw. When multiple exceptions are thrown, separate them with commas.

Note 4:
Starting with JDK7, multiple catch corresponding to the same try can be written together.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The difference between Java exception and error

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.