JAVA notes: basic concepts of exceptions
An exception is a type of command flow that causes program interruption.
Once an exception is generated, the statement after the exception is not executed, it remains in the exception location, and reports the exception to the user.
Basic Format:
Try {
// The statement to be executed
}
Catch (){
// Throw an exception
}
Finally {
// Unique exit of the exception
}
For example:
public class ExceptionTest {public static void main(String args[]){int i = 10;int j =0;try {int temp = i/j;System.out.println(temp);} catch (Exception e) {// TODO: handle exceptionSystem.out.println("exception:"+e);}finally{System.out.println("over");}}}
Inheritance structure of exception classes:
In the Exception structure of Java, the two most common classes are Exception and Error, both of which are subclasses of Throwable.
Exception generally indicates errors in the program. You can use try... catch to solve them;
Error indicates a JVM error that cannot be processed in the program.
Throwable
Class is the superclass of all errors or exceptions in Java. The Java virtual machine or Javathrow
Statement throw. Similarly, only this class or one of its subclasses can becatch
The parameter type in the clause.
When handling an Exception, you can directly use System. out. println () to print the Exception. You can also use the method provided by Exception: public void printStackTrace ()
The exception handling process in Java is as follows:
* If an exception occurs, an instantiated object of the exception class is generated. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + KtTadHJ50 ++ signature + signature/z0 + signature + CjxwPirI57n71 + signature + yk8L3A + Signature = "http://www.2cto.com/uploadfile/Collfiles/20150105/20150105085628441.jpg" width = "320" height = "150" alt = "\">
The Exception type in catch can be directly written as an Exception, which is determined based on the output, but it is best to capture exceptions separately.
The exception statements for capturing coarse particles should be placed after the statements for capturing fine-grained exceptions, like water leaks, sand, and stones. Water leaks, sand, and rock.
Critical throws? Http://www.bkjia.com/kf/yidong/wp/ "target =" _ blank "class =" keylink "> WPC9zdHJvbmc + labels/J0tTKudPDdGhyb3dzudi8/labels + cjxwpsd9yoejuw.vcd4kpha + labels =" brush: java; "> class Math {public int div (int I, int j) throws Exception {int temp = I/j; return temp ;}} public class ExceptionTest {public static void main (String args []) {int a = 10; int B = 0; Math math = new Math (); try {// you must use try. catch exception System. out. println (math. div (a, B);} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}
If throws Exception is also used in the main method, the main method does not handle exceptions. In this case, the tool used to handle exceptions is JVM.
Throw keywords
Throw throws an exception in a program. The exception thrown by throw is an instantiated object of an exception class.
For example:
Public class ExceptionTest {public static void main (String args []) throws Exception {try {throw new Exception ("throwing an Exception");} catch (Exception e) {// TODO: handle exceptionSystem. out. println (e );}}}
Generally, throws and throws are used in combination in actual development. For example, to display all program execution processes and print related exceptions, you must submit all exceptions to the method call for handling,For example:
class Math {public int div(int i, int j) throws Exception {System.out.println("Start");int temp = 0;try {temp = i / j;} catch (Exception e) {// TODO: handle exceptionthrow e;} finally {System.out.println("Over");}return temp;}}public class ExceptionTest {public static void main(String args[]) {Math math = new Math();try {System.out.println(math.div(10, 0));} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
You can also customize the Exception class when using exceptions. You can create a class that inherits the Exception class.
Difference between Exception and RuntimeException:
Exception must be handled using try... catch in the program;
RuntimeException can be handled without try... catch. if an exception is generated, it is handed over to JVM for processing.