compiled exceptions and run-time exceptions
Compile-time exception refers to the program is correct and the external conditions are not satisfied with the exception of Java requirements must be to catch such exceptions or not by compiling
A run-time exception refers to a program that has a bug, such as a null pointer exception array subscript out-of-bounds all belong to a run-time exception
Error and exception
Exception: An exception that the program itself can handle
Error: The program cannot handle errors that occur on the virtual machine itself or when the virtual machine is trying to execute the application, generally do not require program processing, for example: Insufficient memory space, stack Overflow
throws the type of the exception object is explicitly thrown at the declaration of the method
PackageCom.yyx.pratice;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classTestException {/** Not recommended*/ Public Static voidMain (string[] args)throwsFileNotFoundException, IOException {String filepath= "D:" + file.separator + "Exception.txt"; FileOutputStream Fos=NewFileOutputStream (filepath); String content= "Today is 2018/4/8, the weather is very good"; byte[] Array =content.getbytes (); Fos.write (array); Fos.close (); }}
Second, throw to throw an exception object inside a method
PackageCom.yyx.pratice; Public classTestException {/** Not recommended*/ Public Static voidMain (string[] args) {Try { inti = 3; if(I > 4) {System.out.println ("Congratulations, you got it!" "); } Else { Throw NewMyException ("Sorry, arithmetic error")); } } Catch(Exception e) {System.out.println (E.getmessage ()); } }}classMyExceptionextendsException {Private Static Final LongSerialversionuid = 1L; Publicmyexception (String msg) {Super(msg); }}
Third, try.....catch....finally ...
PackageCom.yyx.pratice;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classTestException {/** Recommended*/ Public Static voidMain (string[] args) {String filepath= "D:" + file.separator + "Exception.txt"; FileOutputStream Fos=NULL; Try{fos=NewFileOutputStream (filepath); String content= "Today is 2018/4/8, the weather is very good"; byte[] Array =content.getbytes (); Fos.write (array); } Catch(IOException e) {e.printstacktrace ();//generally write to log here}finally { if(Fos! =NULL) { Try{fos.close (); } Catch(IOException e) {e.printstacktrace (); } } } }}
Iv. the difference between throws and throw
1. Different location: Throws is used on the function, followed by the exception class, can be followed by multiple, and throw is used in the function, followed by the exception object
2. function is different: throws is used to declare the exception, so that the caller only know the problem that the function may occur, can give a pre-processing mode, throw throws the specific problem object,
Execution to throw, the function is over, jumps to the caller, and throws the specific problem object to the caller. That is, when the throw statement exists independently, no other statements are defined below because the
3. Throws indicates that there is a possibility of an exception that does not necessarily occur; throw throws an exception, and a throw throws an exception object.
4. Both are negative ways of dealing with exceptions (the negativity here is not to say that this is not a good idea), just throw or possibly throw an exception, but not by the function to handle the exception, the real handling exception is handled by the upper call of the function
Exceptions and error handling in Java