Content Experiments
Write a class, create a one-dimensional array in its main () method, try to access the array elements in the words, to produce ArrayIndexOutOfBoundsException exception. In the catch clause catches this exception object, and prints "array bounds" information, plus a finally clause and print a message to prove that there really has been implemented.
Using custom exception classes
Equipment inspection station of dangerous goods, if found dangerous goods will be issued a warning. Programming Analog Devices discovery of dangerous goods.
Technical solutions:
DangerException Exgeption write a subclass of the subclass can create an exception object, call the exception object toShow () method outputs "dangerous substance ### products." Machine to write a class that way checkBag (Goods goods) when the goods are found parameter of dangerous goods (goods of isDanger genus of ### is true) DangerException will throw an exception.
In the main program the main class () try part of the method of try-catch statement to make an example of the class call checkBag Machine (Goods goods) method, if the hair is now ### on the handling of dangerous goods in the catch part of try-catch statement dangerous goods.
Lessons Learned
1. The abnormality handling
try
{
//有可能出现异常的语句
}
catch(异常类 异常对象)
{
//编写异常的处理语句
}
finally
{
//一定会运行的代码
}
2.throw与throws
throws: Use throws declared method indicates that this method does not handle the exception, but to call a method for processing at
throw: direct use throw keyword to throw an exception, directly instantiate an object thrown exception class when you can throw
3. Multithreading
Thread class inheritance and use Runnable Interface
Thread class is not shared, Runnable interface to share