Java exception mechanism

Source: Internet
Author: User
Tags finally block terminates throw exception throwable

The exception (Exception) mechanism in Java is very important, the program will inevitably error, the anomaly mechanism can catch errors in the program, to improve the stability and robustness of the program.

Exceptions in Java are divided into checked Exception (non-Runtime Exception) and unchecked Exception (runtime Exception), and all exception classes inherit Exception directly or indirectly. Exception also directly inherit from the Throwable class, Throwable class of direct subclasses also have the error class, but when the error occurs, the program will be directly out of execution, the program can not handle the error, so here focus on the exception class. RuntimeException classes inherit from exception, and run-time exceptions are inherited directly or indirectly from the RuntimeException class. Other inherited exception classes are non-runtime exceptions that can be found during compilation. Run-time exceptions occur at run time, such as arithmetic except 0, which can be passed during compilation, but run-time exceptions are thrown at runtime, and the divisor cannot be zero.

There are two types of exception handling in Java

1. Catching exceptions

The structure is as follows

1 Try2 3 {4 5 //execute code that could produce an exception6 7 }8 9 Catch(runtimeexception e)Ten  One { A  - //handling of caught exceptions -  the } -  - Catch(Exception e)//can be defined to capture multiple exceptions -  + { -  + //handling of caught exceptions A } at  - finally -  - { -  - //after capturing the code to execute, optional, if you have some words will be executed, whether there is no exception in  -}
try-catch-finally

If the exception is caught by a catch, then a catch statement is not executed, but if there is a finally, the statement inside the finally block will be executed.

2. declaration throws an exception

Throws Exception is declared at the method declaration where the method that produces the exception is called, and this method will not process the class exception, but is handled by the caller of the method.

3. Artificial throw exception

Throw an exception directly within the method, throw exception;

It is not recommended for run-time exceptions to be processed.

A custom exception is a subclass that inherits from the exception class and is generally not defined as inheriting from the RuntimeException class, when there is a return statement in the try block, if there is a finally block statement, The Finally block statement is then returned, but if there is a system.exit (0) statement in the try block, then the statements of the end blocks are not executed because System.exit (0) terminates the currently running Java Virtual machine. The program ends execution before the virtual machine terminates.

1 class extends Exception 2 {3      Public myexception (String str) 4     {5         Super(str); 6     }7 }
myexception

Exceptions are used for file processing, input and output stream applications, and so on.

Common types of exceptions are

1, java.lang.NullPointerException: null pointer exception, the reason that the exception occurs is that a reference is NULL, but the program calls a method of the reference.

2. Java.lang.ClassNotFoundException: The specified class could not be found, the class may not be defined.

3, Java.lang.ArithmeticException: Arithmetic exception, such as Divisor is zero.

4, FileNotFoundException: In the file processing, the specified file cannot be found.

5. IOException: Input Output stream exception

6, Sqlexception:sql exception, execute SQL statement error occurred.

There are also many types of exceptions that you will encounter in future programming.

Java exception mechanism

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.