Java study notes, java notes

Source: Internet
Author: User

Java study notes, java notes

Exception
(1) abnormal procedures.
(2) abnormal System
Throwable
| -- Error. Serious problem. We will not handle it. ·
| -- Exception
| -- RuntimeException: An exception occurs during runtime. We need to modify the code.
| -- Non-RuntimeException exception during compilation, which must be handled; otherwise, the program compilation fails.
(3) Handling exceptions:
A: Default JVM Processing
Output The Exception name, cause, location, and other information on the console, but the program cannot be executed.
B: handle it by yourself (capture exceptions)
A: try... catch... finally
Write the processing code by yourself, and the subsequent programs can continue to execute
B: throws
I declare the method that I cannot handle and tell the caller that there is a problem here.

(4) Interview Questions
A: What is the difference between A compilation exception and A runtime exception?
Compilation exceptions must be handled; otherwise, compilation fails.
You can ignore or handle exceptions during runtime.
B: The difference between throw and throws
Throw:
In the method body, the exception object name is followed and only one
Throw throws an exception object, which indicates that an exception must have occurred.
Throws:
In the method declaration, the following is the abnormal class name, which can be multiple
Throws is a possibility of declaring an exception in a method. This exception does not necessarily generate

      

import java.io.*;public class className{  public void deposit(double amount) throws RemoteException  {    // Method implementation    throw new RemoteException();  }  //Remainder of class definition}

 


(5) finally keywords and interview questions
A: finally is used to release resources, and its code will always be executed. Special case: jvm exited before finally execution
B: Interview Questions
A: What are the differences between final, finally, and finalize?
B: If return exists in catch, is it still executed in finally? If executed, before or after return
Yes, before.

C: Deformation of Exception Handling
Try... catch... finally
Try... catch...
Try... catch...
Try... catch... fianlly
Try... finally

D: Note:

Catch cannot exist independently of try.

Adding finally blocks after try/catch is not mandatory.

After try code, neither catch Block nor finally block can be found.

No code can be added between try, catch, and finally blocks.


(6) custom exceptions
Inherited from Exception or RuntimeException. You only need to provide a construction without parameters and a construction with parameters.
(7) Implementation of exceptions
A: The parent method has an exception thrown. The child rewrite method must be less than or equal to the parent exception when throwing an exception.
B: No exception is thrown for the parent method, and no exception is thrown for the subrewrite method.
C: the parent method throws multiple exceptions. The child rewrite method must be less or less than the parent method.

 

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.