Exception handling in the review of Java basic knowledge

Source: Internet
Author: User
Tags square root

Exception handling mechanism

Exceptions are unavoidable in the course of a program's operation, such as the exception of 0 overflow, array overrun, and file not being found, which will prevent the program from running properly in the event of exceptions. In order to strengthen the strong program, we must take into account the possible anomalies and be able to deal with them accordingly.

Java through the object-oriented approach to exception processing, a method in the process of running, once an exception occurs, it will produce an exception object, this process is called throwing, and then will look for the corresponding processing code for processing, this process is called capture. If it is not found, it will terminate the program run.

Exceptions in Java fall into two categories, namely error (Errors) and exceptions (Exception). Error refers to the Java Virtual machine internal release errors, thrown by the Java virtual machine, such as resource exhaustion, etc., usually this kind of situation is not handled. Exception is a general problem caused by programming errors or accidental external factors, such as the square root of negative numbers, access to files not found, and network connection interruption. Such exceptions need to be handled by the programmer.

Class structure for errors and exceptions:

The general so-called exception handling refers to the exception class exception handling, exception class can be divided into runtime exceptions (RuntimeException) and non-runtime exceptions, Rutimeexception is runtime with Java Virtual machine thrown exception, For example: arithmetic operation exception arithmetricexception and array bounds exception arrayindexoutofboundsexception, etc., non-runtime exception, such as input and output exception (IOException), etc. The Java compiler requires the programmer to catch all non-runtime exceptions, otherwise the compilation does not pass, but the runtime exception can not be handled, but for the robustness of the program, it is best to deal with it.

Common run-time exceptions (RuntimeException) are:

· Arithmetricexception: Mathematical calculation anomalies

 · NullPointerException: null pointer exception.

· Negativearraysizeexception: Negative group length exception.

· Arrayoutofboundsexception: Array index out-of-bounds exception.

 · ClassNotFoundException: The class file did not find an exception.

· ClassCastException: Type cast exception.

 · SecurityException: Violation of security principle exception.

exceptions for non-runtimeexception types include:

· Notsuchmethodexception: The method did not find an exception.

 · IOException: Input Output exception.

· Eoexception: File has ended exception.

·  Filenoefoundexception: The file did not find an exception.

  · NumberFormatException: The string is converted to a numeric exception.

 · SQLException: Manipulating database exceptions.

Two ways to handle exceptions:

1. Use Try{}catch (Exception e) {}finally{//unconditional execution}

2. Method name before throws Exception

Custom Exceptions:

The method is to inherit the exception class, usually providing two construction methods for ease of use, one without parameters, and one with parameters.

Cases

 Public class extends exception{      public usernotfoundexception () {                    // Non-parametric construction method     Super();   }     Public Usernotfoundexception (String msg) {   // There is a constructor method     Super(msg)  ;

custom exceptions are used in two ways:

Violation of business logic:

 Public void F1 ()throws  usernotfoundexception{   if(condition not satisfied) {       Throw usernotfoundexception (' condition not satisfied ');   }}

Try--catch () Capture output:

 Public void F2 () {  try{    // business code  }catch(Exception e) {      Throw New Usernotfoundexception (' No word user ');}  }

Exception handling in the review of Java basic knowledge

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.