Java Fundamentals 18

Source: Internet
Author: User
Tags getmessage throwable

1 Exception 1.1 Exception overview
    • Exception: An exception is an error that occurs while the Java program is running.
1.2 Origin of the exception
    • The problem is that a specific transaction in real life can be described in the form of a Java class and encapsulated as an object.
    • In fact, the description of the abnormal situation after the embodiment of the object in Java is an exception.
1.3 Classification of exceptions
    • The superclass of the anomaly system is throwable.
    • Throwable There are 2 subclass error (Errors) and exception (exceptions) below.
    • Exception below are runtimeexception (run-time exceptions) and compile-time exceptions (Exception subclasses remove runtimeexception).

1.4 How the JVM handles exceptions by default
    • If there is a problem with the program and we do not handle it, the JVM will do the default processing.
    • The name of the exception, the reason and the location of the occurrence of information output in the console. and terminate the program.

    • Example:
 Package Com.xuweiwei; /**  @author @version */public class  Exceptiondemo {    publicstaticvoid  main (string[] args) {         int a = 10/0;                System.out.println ("over");}    }
1.5 Exception handling Scheme 1.5.1 try......catch......finally mode
    • Format
Try {  There may be code for the exception;} Catch (Exception name variable) {   handle exception;} finally {  release resources;  }

    • Example:
 PackageCom.xuweiwei;/** * @authorHu Weiwei *@version1.0*/ Public classExceptionDemo2 { Public Static voidMain (string[] args) {Try {            inti = 10/0; } Catch(Exception e) {e.printstacktrace (); } System.out.println ("Over"); }}

    • Example:
 PackageCom.xuweiwei;/** * @authorHu Weiwei *@version1.0*/ Public classExceptionDemo3 { Public Static voidMain (string[] args) {Try {            int[] arr = {1}; System.out.println (arr[1]); }Catch(ArithmeticException e) {System.out.println ("Divisor cannot be 0"); }Catch(indexoutofboundsexception e) {System.out.println ("Array access out of bounds"); } System.out.println ("Over"); }}

1.5.2 Throws Way
    • Usage Scenario: When defining a function method, you need to expose the problem to the caller to handle it. Then the method is identified by throws.

    • Example:
 PackageCom.xuweiwei;/** * @authorHu Weiwei *@version1.0*/ Public classExceptionDemo3 { Public Static voidMain (string[] args) {Try {            int[] arr = {1}; System.out.println (arr[1]); }Catch(ArithmeticException e) {Throw NewRuntimeException ("Divisor cannot be 0" +e.getmessage ()); }Catch(indexoutofboundsexception e) {Throw NewRuntimeException ("Array access out of bounds" +e.getmessage ()); } System.out.println ("Over"); }}
1.6 What is the difference between a compile-time exception and a run-time exception?
    • Compile-time exceptions must be handled.
    • Run-time exceptions can not be handled.
1.7 The difference between throw and throws?
    • Throws
    • ① is used after the method declaration followed by the exception class name.
    • ② can be separated by commas with multiple exception class names.
    • ③ indicates that an exception is thrown and is handled by the caller of the method.
    • ④throws indicates that there is a possibility of an exception and that these exceptions do not necessarily occur.

    • Throw
    • ① is used in the body of the method, followed by the name of the exception object.
    • ② can only throw an exception object name.
    • ③ indicates that an exception is thrown and is handled by a statement within the method body.
    • The ④throw indicates that an exception was thrown, and the throw is bound to throw some kind of exception.
1.8 How do I handle exceptions?
    • Principle: If the problem can be handled internally, use the try, and if you can't handle it, throw an exception with throws.

    • Difference:
    • ① subsequent programs need to be run on the try.
    • ② subsequent programs do not need to be run THORWS.
1.9 Finally keyword 1.9.1 the features of the FINALLY keyword
    • The body of the statement that is finally controlled is bound to execute unless the JVM exits before the finally is executed.
The role of 1.9.2 finally
    • Used to free up resources, which are often encountered in IO stream operations and database operations.

2 File

3 recursion

Java Fundamentals 18

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.