copy from: https://blog.csdn.net/kangguang/article/details/79177336
In development, if to call method written by someone else, if you can know if someone else will write method exception occurs? It is difficult to judge. In view of this situation, Java always allowed behind the method throws keyword external statement which is likely exception occurs, so that the caller when calling the method, you know exactly which abnormal, and must be abnormal in the program , otherwise unable to compile.
The code below
www.kangxg.jdbc Package;
public class Example {
public static void main (String [] args) {
// the TODO Auto-Generated Method Stub
int Result Divide = (4,2);
System.out.println (Result);
}
static int Divide public (int X, Y int) throws Exception
{
int Result = X / Y;
return Result;
}
}
displays error messages Unhandled exception type Exception this time compiler
so it is necessary to try to call Divide () method. ..catch processing
package www.kangxg.jdbc;
public class Example {
public static void main(String[] args) {
try {
int result = divide(4,2);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
public static int divide(int x,int y) throws Exception
{
int result = x/y;
return result;
}
}
debug 运行程序
When calling divide () method, if you do not know how to handle exceptions thrown statement, you can also use the keyword throws continue to throw an exception, so that the program can be compiled to run. But note that the program once the exception, if not handled, the program will terminate abnormally. as follows:
package www.kangxg.jdbc;
public class Example {
public static void main(String[] args) throws Exception {
int result = divide(4,0);
System.out.println(result);
}
public static int divide(int x,int y) throws Exception
{
int result = x/y;
return result;
}
}
debug run the program
Java run-time and compile-time Exceptions Exception
1. Compile an exception
In Java, Exception class in addition to RuntimeException and its subclasses are compile-time exception. Compile-time unusual feature is the Java compiler will be checked, if there is an exception to an exception must be treated, otherwise the program can not compile.
Approach
Use try ... catch statement to capture abnormal
Use throws keyword to declare an exception is thrown, the caller process it
2. The runtime exception
RuntimeException and its subclasses abnormal operation. Runtime exception is characterized by a Java compiler does not check it. That is, when such an exception occurs in a program, even without the use try ... catch statement throws captured using keyword statement thrown. Program can compile. Runtime exceptions are generally caused by a logic error in the program, and can not be repaired in the program is running. For example, data values out of range.
Three custom exception
JDK defined in a number of exception classes, the most unusual circumstances While these exception classes can describe the program, but may sometimes need to describe the program specific anomalies in program development. E.g. Divide () method does not allow the dividend is negative. To solve this issue by allowing the user to custom exception in Java, but the custom exception class must inherit from Exception or subclass. Examples are as follows
package www.kangxg.jdbc;
public class DivideDivideByMinusException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public DivideDivideByMinusException(){
super();
}
public DivideDivideByMinusException(String message)
{
super(message);
}
}
package www.kangxg.jdbc;
public class Example {
public static void main(String[] args) throws Exception {
try {
int result = divide(4,-2);
System.out.println(result);
} catch (DivideDivideByMinusException e) {
System.out.println(e.getMessage());
}
}
public static int divide(int x,int y) throws DivideDivideByMinusException
{
if(y<0)
{
throw new DivideDivideByMinusException("被除数是负数");
}
int result = x/y;
return result;
}
}
————————————————
Disclaimer: This article is the original article's "not entirely clear when the rain" CSDN bloggers, follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/kangguang/article/details/79177336