10 / 13-10 / 19 java learning summary (no classroom after school learning summary) & Experiment Summary 6
Experiments six Java exception
- Purpose
- Understand the basic concepts abnormal;
- Master and exception handling are familiar with common method to capture the exception.
- Experimental requirements
- Practice catch the exception, unusual statement, the method throws an exception, familiar with the try and catch clauses.
- Master the method custom exception class.
- 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 goods." Machine to write a class that way checkBag (Goods goods) when the goods are found parameter of dangerous goods (goods of isDanger property is true) DangerException will throw an exception.
In the main program the main class () try part of the method of try-catch statement let instance of the Machine class checkBag (Goods goods) method call, if found dangerous goods on the part of the handling of dangerous goods in the catch try-catch statement.
package id; import java.util.*; public class hope { public static void main(String[] args) { int score[]=new int [9]; Scanner out = new Scanner(System.in); int n = out.nextInt(); try{ for(int i=0;i<9;i++){ score[i]=i+1; } System.out.println("score["+n+"]="+score[n]+" "); }catch(ArrayIndexOutOfBoundsException a){ System.out.println("越界:"+a); }finally{ System.out.println("处理完毕"); } } }
package id; class DangerException extends Exception{ public void examination() { System.out.println("易燃易爆危险物品"); } } class thing{ private boolean Danger; private String name; public Object getName; public Goods(boolean Danger,String name) { this.Danger=Danger; this.name=name; } public boolean Danger() { return Danger; } public void setName(String name) { this.name=name; } public String getName() { return name; } } class Machine{ public void checkBag(thing goods)throws DangerException{ if(goods.Danger()) throw new DangerException(); else System.out.println(goods.getName()+"不是易燃易爆危险品"); } } public classHope { public static void main (String [] args) { Machine MAC = new new Machine (); Thing Goods = new new Goods ( to false , null ); String A [] = { "C4", "lighter", "laptop" }; for ( int I = 0; I <a.length; I ++ ) { goods.setName (A [I]); IF (A [I] .equals ( "C4") || A [I] .equals ( "lighter" )) { goods.Danger (); System.out.println (A [I] + "is inflammable and explosive dangerous goods" ); } else goods.Danger(); } try { mac.checkBag(goods); }catch(DangerException e) { e.examination(); } } }
summarize
2.throws indicate a possible abnormal (not necessarily the occurrence of these anomalies ), throwthrow is certainly some kind of thrown exception object.
Inherit the Thread class, is to divide tasks to each object were completed, because MyThread inherit the Thread class, so when new MyThread create threads at the same time to create the object;
Implement Runnable, it is the task given to multiple objects together to complete, new MyThread equivalent to creating a task, and then instantiate multiple objects to create a thread that is scheduled to perform window.
try and catch keywords are keywords used to capture abnormal
Catch statement to catch exceptions include the type of statement. When an exception occurs in the protected code block, try later catch block will be checked.
Throwable difference Exception class and subclass of class RuntimeException
1, Exception in the program is a must try ... catch is processed.
2, RuntimeException can not use try ... catch is processed, but give the JVM appear abnormal processing