Abnormal
An exception is a flow of instruction that causes the program to run, and if the exception is not handled correctly, it can cause the program to break execution, causing unnecessary loss.
Therefore, in the design of the program must consider the occurrence of various anomalies, and correct the corresponding treatment, so as to ensure that the normal implementation of the program.
Inheritance structure for exception classes
In the entire Java exception structure, there are actually the following two most commonly used classes: Exception, Error, these two classes are all throwable subclasses
Exception: Generally represents a problem in the program, you can directly use the Try...catch processing.
Error: General refers to a JVM error that cannot be handled in the program.
Custom exception Classes
You can complete a custom exception class only if you inherit exception. Because the standard exception classes are provided in Java (including some exception information, etc.)
You can customize the exception class if you need to define the exception information you want.
PackageCom.Itmyhome;ImportJava.util.Scanner;ClassMyExceptionExtendsException{Custom exception ClassesPublicMyException(StringName){Super(Name);Call the parent class construct}}PublicClassT3{/*** @param args*/PublicStaticvoidMain(String[]Args){TODO auto-generated Method StubScannerScan=NewScanner(System.Inch);System.Out.Print("Please enter the divisor:");IntI=Scan.Nextint();System.Out.Print("Please enter dividend:");IntJ=Scan.Nextint();Try{If(J==0){Thrownew myexception ( "dividend cannot be 0" //throws a custom exception } else {systemout. (i /j); } } catch (exception e) {e. Printstacktrace (); finally { }
Several common interview questions: 1, try {} has a return statement, then immediately after this try finally{} code will be executed, when executed, before or after return?
Will execute before the return.
See below for two examples
PublicClassT{PublicStaticvoidMain(String[]Args) {system. Out. (new t (). static int test () {int x = 1try {return x} finally {++x} }
Output returned 1
PublicClassT{PublicStaticvoidmain (string[] args ) {//TODO auto-generated Method stub systemout. (test ()); public static int test () {try{return 1}finally{return 2 } } } /span>
Output returned 2
The function called by the return statement in try executes before the function called in Finally, that is, the return statement executes first, and the finally statement executes, so the return result is 2.
Return does not return the function immediately, but the return statement executes, and the returned result is placed in the function stack, where the function is not returned immediately, to be executed
The finally statement does not actually begin to return.
2, the difference between final,finally,finalize
Final is used to declare properties, methods, and classes, respectively, that the property is immutable, that the method is not overridden, and that the class is not inheritable.
Finally is part of the exception-handling statement structure, which indicates that it is always executed.
Finalize is a method of the object class that, when executed by the garbage collector, calls this method of the reclaimed object, which can override this method to provide garbage collection
Other resource recycling, such as closing files. The JVM does not guarantee that this method is always called.
3. What are the similarities and differences between abnormal operation and general anomaly?
An exception represents an unhealthy state that may occur during a program's run, and a run-time exception that represents an exception that may be encountered in a typical operation of a virtual machine is a common run error.
The Java compiler requires the method to declare a non-runtime exception that might occur, but does not require that a runtime exception that is not caught to be thrown must be declared.
4. What is the difference between error and exception?
Error indicates a serious problem in situations where recovery is not impossible but difficult. such as memory overflow, it is impossible to expect the program to come out of this situation.
Exception represents a design or implementation issue.
That is, it means that if the program runs normally, it never happens.
5, write out the most common 5 runtime exception
Classcastexception,indexoutofboundsexception,nullpointerexception,arithmeticexception,illegalargumentexception
6. Simple principle and application of exception handling mechanism in Java
Exceptions are abnormal conditions or errors that occur when a Java program is run (not compiled), and Java uses an object-oriented approach to handle exceptions, which takes each
Exceptions are also encapsulated into an object that contains information about the exception.
Java classifies exceptions, different types of exceptions are represented by different Java classes, and the root class for all exceptions is java.lang.throwable,throwable and
Two subclass error and Exception,error are derived to represent a serious problem that the application itself cannot overcome and recover, such as memory overflow and thread deadlock, etc.
System problems. Exception indicates that the program can also overcome and recover the problem, which is divided into system anomalies and common anomalies, system anomalies are the software itself caused by defects
Problem, which is caused by poor software developers ' consideration, software users cannot overcome and recover from this problem, but in this case it is also possible to make the software system
Keep Running or let the software die, such as array out of bounds (arrayindexofoutofboundsexception) null pointer exception (NULLPOINTEREXCEPTION)
Class Conversion Exception (ClassCastException) A common exception is a problem caused by the number or exception of a running environment, a problem that users can overcome such as a network disconnection,
There is not enough disk space, and after such an exception, the program should not die.
Java provides a different solution for system exceptions and common exceptions, and the compiler enforces common exceptions that must be try...catch to handle the goods with throws declarations continue to be thrown at the upper calling method
, so common anomalies are also known as checked anomalies, two system exceptions can be handled or not handled, so the compiler does not enforce try...catch processing or throws sound
System anomalies are also known as unchecked anomalies.
Copyright NOTICE: This article uses the BY-NC-SA agreement to authorize, reproduced the Wheat Field Technical blog article please indicate the source
Original address: Http://itmyhome.com/2015/03/java-study-notes-exception
Java Learning Note 16--exception