11th Chapter Exception Handling
I. Classification of anomalies
1, controllable type exception:
(1) IOException: When sending some kind of I/O exception;
(2) SQLException: Exceptions to database access or other errors
(3) ClassNotFoundException: Class not found exception
(4) Nosuchfieldexception: Exception when class does not contain a field with the specified name
(5) Nosuchmethodexception: An exception cannot be found for a method
1 Public classEx1 {2 3 Private intnum=10;4 Public intGetnum () {5 return This. Num;6 }7 Public voidSetnum (intnum) {8 This. num=num;9 }Ten PublicEx1 () { One Try{ AClass.forName ("Ex123");//attempt to load class -}Catch(ClassNotFoundException e) {//Catching exceptions -E.printstacktrace ();//the output method of the exception the } -SYSTEM.OUT.PRINTLN ("Test! "); - } - + Public Static voidMain (string[] args) { - //TODO auto-generated Method Stub +Ex1 test=NewEx1 (); ATest.setnum (888); at System.out.println (Test.getnum ()); - } -}
2. Run-time exception
(1) Indexoutofboundsexception: The index of the collection or array is out of range exception
(2) NullPointerException: The program uses a null exception when the object is needed
(3) ArithmeticException: Anomalies in mathematical operations
(4) IllegalArgumentException: The method passed the illegal parameter
(5) ClassCastException: Forcing an object to be replaced by an exception that is not a subclass of an instance
1 Public classExc2 {2 int[] number={100,80,50,70,20,60};3 Public voidSetnum (intIndexintvalue) {4number[index]=value;5 }6 Public intGetnum (intindex) {7 returnNumber[index];8 }9 Public Static voidMain (string[] args) {TenExc2 test=NewExc2 (); One for(inti=0;i<=10;i++){ A Try{ - System.out.println (Test.getnum (i)); -System.out.println ("+i+" array element is displayed normally "); the}Catch(Exception e) { - e.printstacktrace (); -System.out.println ("+i+" element cannot be read!) "); - Break; + } - } + } A}
Second, handling exceptions
For statements that may occur in a program, you can add them to a try statement block and handle them appropriately with a catch statement block. There are try-catch statements, and try-catch-finally statements.
Where the finally statement block is the code that executes regardless of whether the program in the TRY statement block has an exception.
Third, practical practice
1 /**2 * Write an exception class myexception, and then write a student class with an speak (int m) method that produces an exception3 * When M is greater than 1000, the method throws the MyException object4 * Through the main class test5 * @authorTS6 */7 Public classtest{8 Public classMyExceptionextendsexception{9 PublicMyException (inti) {TenSystem.out.println ("The value of the exception!i cannot be greater than 1000!") "); OneSystem.out.println ("Current I value is" +i); A } - } - Public classstudent{ the Public voidSpeakintMthrowsmyexception{ - if(m>1000){ - Throw Newmyexception (m); -}Else{ +SYSTEM.OUT.PRINTLN ("No Exceptions found!") "); - } + } A } at Public Static voidMain (string[] args) { -Test test=NewTest (); -Student Stu=test.NewStudent (); - Try{ -Stu.speak (1001); -}Catch(MyException ex) { in ex.printstacktrace (); - } to } +}
1 /**2 * Create a number class, with the count () method of the class to get the result of any two numbers added3 * If a negative number is passed to the count () method, a custom exception is thrown4 * Test in the main class5 * @authorTS6 */7 Public classTest {8 Public classnumber{9 LongCountLongMLongNthrowsmyexception{Ten if((m<0) | | (n<0)){ One Throw Newmyexception (); A}Else{ -System.out.println ("return correctly! The result is "+ (m+n)); - returnm+N; the } - } - } - Public classMyExceptionextendsexception{ + Publicmyexception () { -System.out.println ("throws a custom exception here myexception! "); +System.out.println ("Reminder: There are negative numbers in Addend! "); A } at } - Public Static voidMain (string[] args) { -Test test=NewTest (); -Number Num=test.NewNumber (); - Try{ -Num.count (12,-15); in}Catch(MyException e) { - e.printstacktrace (); to } + - } the *}
"Java from Beginner to Mastery" Chapter 11th study notes