Exception Handling notes summary

1. Handling Exceptions

grammar:

try
{
    程序代码1
}
catch(异常类型 异常的变量名)
{
    程序代码2
}
finally
{
    程序代码3
}

Description:

  • If there is an exception code runs were captured catch, a skip program code, executing the program code 2 and 3; abnormal operation does not occur if the program code is 1, 1 and 3 execute the program code, the program code for skipping 2;
  • On module principle finally can not write, writing aims to increase readability.
  • the catch () can be interpreted as the parameter in the assignment; <Exception type> = uncaught exception exception variable;

2. thrown

throws Throws:

void 方法名(type 参数) throws Exception
{

}

throw Throws:

try
{
    thow ArrayIndexOutOfBoundsException();
}
catch(ArrayIndexOutOfBoundsException e)
{
    thow e;
}

Description: throwing an exception is thrown on abnormal or abnormal reference level, usually with try catch statement to catch.

3. custom exception (to be learned)

  • Create an exception only need to inherit his Thorwable class or subclass of Exception.
  • Java.lang.Throwable class is the base class for all exception classes, which comprises two subclasses: Exception and Error.
  • Exception class for describing the program exception can be captured. Error JVM is unexpected error. (E.g., forced shutdown program is running)

Guess you like

Origin www.cnblogs.com/weiyi2020/p/12368617.html