I. Exceptions 1. Overview of exceptions
What is an exception: An unknown event that occurs during a program run that can interrupt the normal execution of a program instruction.
View of exception classes in Java:
Description: The subclass of error and exception can be divided into several classes, not all subclasses.
Throwable:
The Throwable class is a superclass of all errors or exceptions in the Java language. A Java virtual machine or throw statement can only be thrown if the object is an instance of this class (or one of its subclasses).
Error:
Error is a subclass of Throwable that is used to indicate a serious problem that a reasonable application should not attempt to capture. It is an exception that the program cannot handle, and is generally unrelated to the programmer's execution. In theory, these errors are not allowed to occur, and should not be attempted through the program, so error is not a Try-catch object, and the JVM typically handles terminating the thread where the error occurred. Common subclasses of the error class have Virtualmachineerror and Awterror.
Exception:
The Exception class and its subclasses are a form of throwable that identifies the conditions that a reasonable application wants to capture. The reason for the occurrence depends on the program, so the program should be processed through Try-catch.
Exceptions fall into two categories: exceptions can be checked (compile-time exceptions) and non-checked exceptions (run-time exceptions).
exceptions can be checked:
Compiler requirements must be handled, otherwise it cannot be compiled, using Try-catch capture or throws thrown. Common IOException (IO errors) and their subclasses eofexcption (file ended exception), FileNotFound (file not found exception).
Non-check exception:
The compilation period is not checked, so it is not processed in the program, but if it does, it will be thrown at run time. So this kind of anomaly should be avoided as far as possible! Common non-checked exceptions are the RuntimeException class and its subclasses.
2. Declaring Exceptions:
Declaring with the throw keyword
1 Packagecom.wx.throwable;2 Importjava.io.FileNotFoundException;3 Importjava.io.IOException;4 5 /**6 * @className Test Exception class7 */8 Public classTest {9 Public Static voidMain (string[] args)throwsfilenotfoundexception,ioexception {Ten Try { One Throw NewException ("This is the declared exception"); A}Catch(Exception e) { - e.printstacktrace (); - } the } - -}
3. Exception handling: Try Catch finally or throws catch exception (try Catch finally):
The following scenarios occur where the code in the finally is not executed:
1) An exception was thrown in the finally statement block and is not processed.
2) exit the program with System.exit () in the previous code.
3) The thread on which the program is located dies.
4) The CPU is turned off abnormally.
Examples of handling Exceptions:
1 Packagecom.wx.throwable;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 Importjava.io.FileNotFoundException;6 Importjava.io.IOException;7 8 Public classTest {9 Public Static voidMain (string[] args) {TenFileInputStream fis=NULL; One Try { A //code that may appear to be abnormal -fis=NewFileInputStream ("D:/abc.txt"); - } the Catch(FileNotFoundException e) { - //Handling Exceptions - e.printstacktrace (); - } + finally{ - if(fis!=NULL) { + Try { A //Freeing Resources at fis.close (); -}Catch(IOException e) { - e.printstacktrace (); - } - } - } in } -}
try-catch-finally Handling Exceptions
As you can see from the example above, the try-catch-finally statement can be nested.
Throws
The exception is handled by throwing it outward, allowing the next caller to handle the exception, and if the main method is thrown out of the way, it will be handed to the program for processing. In this way, only the compile-time exception is filtered out, and since there is no final processing, the program will be interrupted once an exception occurs.
Examples of throws handling exceptions:
1 Packagecom.wx.throwable;2 3 ImportJava.io.FileInputStream;4 Importjava.io.FileNotFoundException;5 Importjava.io.IOException;6 7 Public classTest {8 Public Static voidMain (string[] args)throwsfilenotfoundexception,ioexception {9FileInputStream fis=NewFileInputStream ("D:/abc.txt");Ten if(fis!=NULL) { One fis.close (); A } - } -}
Throw handling Exception4. Custom Exceptions
To create a custom exception class myexception inherit exception:
1 Packagecom.wx.throwable;2 3 Public classMyExceptionextendsException {4 5 Publicmyexception () {6 7 }8 Publicmyexception (String msg) {9 Super(msg);Ten } One}
Using Exceptions:
1 Packagecom.wx.throwable;2 Importjava.io.FileNotFoundException;3 Importjava.io.IOException;4 5 /**6 * @className Test Exception class7 */8 Public classTest {9 Public Static voidMain (string[] args)throwsfilenotfoundexception,ioexception {Ten Try { One NewTest (). Throwmyexception (); A}Catch(MyException e) { - e.printstacktrace (); - } the } - - /** - * Throw a custom exception + * @throwsmyexception - */ + Public voidThrowmyexception ()throwsmyexception {//throw it out . A Throw NewMyException ("error");//Throw Exception at } - -}
Java Intermediate Exception Chapter