java--Exceptions and file handling

Source: Internet
Author: User

1: Exception
(1) Abnormal situation of the program.
(2) Abnormal system
Throwable
|--error Serious problem that we do not deal with.
|--exception
|--runtimeexceptionRun-time exception, we need to fix the code
|--non-RuntimeException compile-time exception, must be handled, or program compilation does not pass
(3) Handling of exceptions:
Default handling of A:JVM
The exception name, reason, location and other information output in the console, but the program can not continue to execute.
B: Handle It Yourself
A:try...catch...finally
Write your own processing code, the following program can continue to execute
B:throws
You can't handle it, declare it on the method, tell the caller, there's a problem.
(4) Face test
A: What is the difference between a compile-time exception and a run-time exception?
Compile-time exception must be handled, otherwise compilation does not pass
Run-time exceptions can be handled without processing or
B:throw and throws, that's the difference.
Throw
In the method body, followed by the exception object name, and can only be a
Throw throws an exception object, which means there must be an exception here.
Throws
On the method declaration, followed by the class name of the exception, can be multiple
Throws is a method of declaring an exception, which is a possibility, and this exception does not necessarily produce
(5) Finally keyword and its face question
A:finally is used to release resources, and its code is always executed. Special case: The JVM exited before executing to finally
B: Face question
The difference between a:final,finally,finalize?
B: If there is a return in the catch, will the finally be executed? If executed, before or after return
Will, before.

Actually in the middle. We talked about this class.
C: Deformation of exception handling
Try...catch...finally
Try...catch ...
Try...catch...catch ...
Try...catch...catch...fianlly
Try...finally
(6) Custom exceptions
Inherited from exception or runtimeexception, it is only necessary to provide a non-parametric construction and a parameter structure.
(7) The realization of abnormal attention
A: The parent's method has an exception thrown, and the child's overriding method must be less than or equal to the parent's exception when throwing an exception
B: The parent's method does not throw an exception, and the child's overriding method cannot have an exception thrown
C: The parent's method throws more than one exception, and the child's overriding method must be less or smaller than the parent


2:file (Master)
(1) Most of the IO flow operations are operations on files, so Java provides the file class for us to manipulate the files
(2) Construction method
A:file file = new file ("E:\\demo\\a.txt");
B:file file = new file ("E:\\demo", "a.txt");
C:file file = new file ("E:\\demo");
File File2 = new file (file, "A.txt");
(3) function of the file class (self-completion)
A: Create A feature
B: Delete Feature
C: Rename function
D: Judging function
E: Get Features
F: Advanced Access function
G: Filter function
(4) Case:
A: Output The file name of the specified suffix name in the specified directory
A: Get all first, judge at the time of traversal, and then output
B: First judge, then get, and finally directly traverse the output can

B: Batch modification of file names



Let's look at a simple example:

Import Java.text.parseexception;import Java.text.simpledateformat;import java.util.date;/* * Exception Note: * A: When subclasses override parent class methods, The method of the subclass must throw the same exception or subclass of the parent exception. (father is broken, son can not be worse than father) * B: If the parent class throws more than one exception, when the subclass overrides the parent class, it can only throw the same exception or a subset of it, the subclass cannot throw an exception that is not the parent class * C: If the overridden method does not throw an exception, the method of the subclass must not  If an exception occurs within a subclass method, then the subclass can only try, not throws */public class Exceptiondemo {}class Fu {public void Show () throws Exception {}public void Method () {}}class Zi extends Fu {@Overridepublic void Show () throws ArithmeticException {} @Overridepublic void method () { String s = "2014-11-20";//SimpleDateFormat SDF = new SimpleDateFormat ();//Date D = sdf.parse (s);//System.out.println (d);}}



java--Exceptions and file handling

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.