Exception, Throwable, finally, File Class (19)

Source: Internet
Author: User

1. Overview and classification of anomalies

* A: Overview of exceptions
* Exceptions are errors that occur during the operation of the Java program.
* B: Classification of anomalies
* View Throwable via API
* Error
* Server downtime, database crashes, etc.
* Exception
C: Abnormal inheritance system
* Throwable
* Error
* Exception
* RuntimeException

2.JVM The default is how to handle exceptions

*: How the JVM handles exceptions by default
* When the main function receives this problem, there are two ways to handle it:
* A: Handle the problem yourself, then continue running
* B: There is no treatment for yourself, only the JVM that calls main to handle
* The JVM has a default exception handling mechanism that handles the exception.
* and the name of the exception, the exception information. The location where the exception occurred is printed on the console, and the program stops running

3.try...catch Way to handle exceptions

* A: Two ways of exception handling
* a:try...catch...finally
* B:throws
* B:try...catch basic format for handling exceptions
* try...catch...finally

4. Differences between compile-time exceptions and run-time exceptions

*: What is the difference between a compile-time exception and a run-time exception?
* Exceptions in Java are divided into two main classes: compile-time exceptions and runtime exceptions.
* All instances of the RuntimeException class and its subclasses are called run-time exceptions, and other exceptions are compile-time exceptions.
* Compile-time exception
* The Java program must show processing, or the program will have an error and cannot be compiled.
* Run-time exception
* No display processing is required, and can be treated as a compile-time exception.

* compile-time exception and runtime exception    difference * Compiler exception      * Prepare for a rainy      Day * When you're ready to do something    , think ahead of time what might happen * run-time     exception * Compile without error, the program run up if there are errors will be reported abnormal, run-time exception is the programmer's mistakes, need to come back to modify the code

Several common methods of 5.Throwable

*: Several common methods of throwable
* A:getmessage ()
* Get exception information, return string.
* B:tostring ()
* Gets the exception class name and exception information, and returns a string.
* C:printstacktrace ()
* Gets the exception class name and exception information, as well as the location (line number) where the exception appears in the program. return value void.

Handling Exceptions in 6.throws mode

*: Throws way to handle exceptions
* When defining a function method, you need to expose the problem to the caller to handle it.
* Then the method is identified by throws.

7.throw Overview And what's the difference from throws?

* Overview of A:throw
* In the function method within a certain situation, the program can not continue to run, need to jump, with throw to throw the exception object.

* The difference between b:throws and throw
* A:throws
* Used after the method declaration, followed by the exception class name
* Can be separated by commas with multiple exception class names
* Indicates a thrown exception, handled by the caller of the method
* B:throw
* Used in the body of the method, followed by the name of the exception object
* Only one exception object name can be thrown
* Indicates thrown exception, handled by the statement in the method body

Features and functions of 8.finally keywords

* Features of A:finally
* The statement body is bound to be executed by the finally control.
* Special case: The JVM exits before execution to finally (e.g. System.exit (0)).
* The role of b:finally
* Used to release resources, as seen in IO stream operations and database operations.

the face question of the finally keyword?  Whatis the difference between final, finally, and finalize?     Final        * The modified class cannot be inherited, the modified method cannot be overridden, the modifier variable cannot be changed, the modifier of the base type is constant, and the modified reference data variable does not change its address value.       finally        * release resources.      * Finalize ()         * is a method that is called at garbage collection time.
* If there is a return statement inside the catch, will the finally code be executed? If yes, will it be before or after return?     Execute before return .

9. Overview and basic use of custom exceptions

* A: Why do I need A custom exception
* Example: Age of person
* B: Overview of custom exceptions
* Inherit from exception
* Inherit from RuntimeException

10. Exception precautions and how to use exception handling

* A: Exception precautions
* A: When a subclass overrides a parent class method, the child class's method 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 his, the subclass cannot throw exceptions that the parent class does not have
* C: If the overridden method does not throw an exception, then the method of the subclass must not throw an exception, if there is an exception in the subclass method, then the subclass can only try, not throws
* B: How to use exception handling
* Principle: If the function can be handled within the problem, with a try, if not handled, referred to the caller, which is used throws
Difference
* Subsequent programs need to be run on try
* Subsequent programs do not need to continue to run throws.
* If the JDK does not provide a corresponding exception, a custom exception is required.

Overview of the 11.File class and how to construct it

* Overview of the A:file class
* Abstract representation of file and directory path names.
* file should be called a path
* File path or folder path
* Path is divided into absolute path and relative path
* Absolute path is a fixed path, starting with the drive letter
* Relative path relative to a position, under Eclipse refers to the current project under DOS, which refers to the current path
* View API
* Abstract representation of file and directory path names
* B: Construction method
* File (String pathname): Gets the file object based on a path
* File (string parent, String child): Gets the file object based on a directory and a sub-file/directory
* File (file parent, String child): Gets the file object based on a parent file object and a sub-file/directory.

How to construct the file class/*1. Get the file object according to a path * file File = new file ("F:\\java basic class \ \ Basic class video and code \\day18java basic Class \\video");//The path is with a \ Slash, a single/also can.          Boolean B = file.exists (); the exists () in the//file class is to determine if the file exists. System.out.println (b);//true existence*/            /*2. Get the file object according to a directory and a sub file/directory * String parent = "F:\\java basic class \ \ Basic class video and Code";          String child = "Day18java basic class \\video";          File File = new file (parent,child);          System.out.println (File.exists ()); */        /*3.: Get file Object According to a parent file object and a sub file/directory * File parent = new file ("F:\\java basic class \ \ Basic class video and Code");           String child = "Day18java basic class \\video";           File File = new file (parent,child);        System.out.println (File.exists ()); */

12.File class creation capabilities

*: Create Feature
* Public boolean CreateNewFile (): Create file If such a file exists, you do not create a
* Public boolean mkdir (): Create folder if such a folder exists, you do not create a
* Public boolean mkdirs (): Create a folder, if the parent folder does not exist, will help you create it

the CREATE function        of the file class /* 1. Create a file if such a file exists, the * file file         = new file ("MyWorld") is not created;           Boolean B = File.createnewfile ();//Create a file under current project           System.out.println (b);         */        /* 2. Create a folder if such a folder exists, the         * file File = new file ("Mygirl") is not created;//Create Folder          System.out.println (File.mkdir ());         */        *3. Create a folder, if the parent folder does not exist, will help you to create a         * file File = new file ("World\\girl");          System.out.println (File.mkdirs ());//create Multi         -level folders *     * Note:        * If you create a file or folder and forget to write the drive path, the default is under Project path.

Delete function for 13.File class

* A: Delete function
* Public Boolean Delete (): Delete file or folder
* B: Case Demo
* Delete Function of file class
* Look down.
* Precautions:
* Delete in Java does not go to recycle Bin.
* To delete a folder, please note that the folder cannot contain files or folders.

Renaming of the 13.File class

*: Rename function
* Public boolean Renameto (file dest): Renames files to the specified file path.

Rename function        for file class /* 1. Delete the file or folder, delete the folder, must be an empty folder, after deletion does not go through the Recycle Bin         * File File = new file ("Xxx.txt");           System.out.println (File.delete ());         */        /* 2. Rename the file to the specified file path         * 1) If the path name is the same, it is renamed         * File File = new file ("Xxx.txt");           File FILE0 = new file ("zzz");            System.out.println (File.renameto (FILE0));         * 2) If the path name is different is cut and renamed           file File = new file ("F:\\java basic class \ \ Basic class video and code \\day18java basic Class \\aaa");           File FILE0 = new file ("zzz");           System.out.println (File0.renameto (file));         */        * Note        :* If the path name is the same, it is renamed.         * If the path name is different, it is renamed and cut.

The judgment function of the 14.File class

*: Judging function
* Public boolean isdirectory (): Determine if it is a directory
* Public boolean isfile (): Determines whether the file is
* Public Boolean exists (): Determine if there is
* Public boolean CanRead (): Determines whether it is readable
* Public boolean CanWrite (): Determines if writable
* Public boolean Ishidden (): Determines whether to hide

File class's judgment function file file=NewFile ("MyWorld"); File FILE0=NewFile ("World"); File file1=NewFile ("F:\\java basic class \ \ Basic class video and Code"); System.out.println (File.isdirectory ());//falseSystem.out.println (File0.isdirectory ());//trueSystem.out.println (File.isfile ());//trueSystem.out.println (File0.isfile ());//falseSystem.out.println (File.exists ());//trueFile file2=NewFile ("MyWord"); File2.setreadable (false); System.out.println (File2.canread ());//all the files under the True,windows system are readable. File File3=NewFile ("MyWorld"); File3.setwritable (false); System.out.println (File3.canwrite ());//false,windows system, can be set to non-writableFile file4=NewFile ("F:\\java basic class \ \ Basic class video and code \\ddd.txt"); System.out.println (File4.ishidden ());//true to determine if it is hidden

15.File Class Acquisition function

*: Get Features
* Public String GetAbsolutePath (): Gets the absolute path
* Public String GetPath (): Get relative path
* Public String GetName (): Get Name
* Public long Length (): Gets the length. Number of bytes
* Public Long LastModified (): Gets the last modified time, millisecond value
* Public string[] List (): Gets an array of names for all files or folders under the specified directory
* Public file[] Listfiles (): Gets the file array of all files or folders under the specified directory

get function of file class file file=NewFile ("World"); //F:\eclipseTest\day19_0\World, gets the absolute path,System.out.println (File.getabsolutefile ()); //World , Get relative pathSystem.out.println (File.getpath ()); //World , get the nameSystem.out.println (File.getname ()); //0, get the length, the length of the folder is 0; the length of the file is equal to the byte code it occupies .System.out.println (File.length ()); File file1=NewFile ("Mygirl.txt");        System.out.println (File1.length ()); //lastmodified () Gets the last modified time, from 1970 to the current millisecond valueSystem.out.println (File1.lastmodified ()); LongTime =file1.lastmodified (); Date D=NewDate (time);//converts the millisecond value to the current date (the date when the last modification was made)System.out.println (d); //Public string[] list (): Gets an array of names for all files or folders under the specified directory//it only outputs the name. File file2 =NewFile ("F:\\java basic class \ \ Basic class video and Code"); String[] s=file2.list ();  for(String str:s) {System.out.println (str); }        //Public file[] Listfiles (): Gets the file array of all files or folders under the specified directory//It also outputs the path together. File File3 =NewFile ("F:\\java basic class \ \ Basic class video and Code"); File[] F=File3.listfiles ();  for(File file4:f) {System.out.println (file4); }

16. Overview and use of file name filters

* A: Overview of file name filters
* Public string[] List (filenamefilter filter)
* Public file[] Listfiles (filenamefilter filter)
* B: Use of File name filter
* Requirements: Determine if the e-disk directory has a suffix. jpg file, and if so, output the file name
* C: Source code Analysis
* The source code of the list () method with the file name filter.

17.File Class (Recursive)

* 1. Cons: Easy stack Memory overflow* 2. Benefits: No need to know the number of calls* 3. Constructors cannot be recursive* 4. The return value can or may not be. Factorial of 5 Public Static voidMain (string[] args) {System.out.println (5)); }         Public Static intFunintnum) {            if(num = = 1){                return1; }Else{Renturn num* Fun (num-1); }        }

Exception, Throwable, finally, File Class (19)

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.