An exception is an event that occurs when a program executes, and it interrupts the normal flow of instructions. Java provides a unique mechanism for handling exceptions that are handled by exceptions to the errors that occur in the design of the program. A Java exception is an object that describes an exception (an error) that occurs in a code snippet.
Throws an exception: An exception object is generated when an exception event occurs during the execution of a Java program . The generated exception object is passed to the Java Runtime System , and the resulting and committed process of this exception is called throwing an exception.
Category: JRE (System) generates an exception object artificially generating an exception object (explicitly with the throw keyword)
Exception handling mechanism:
two mechanisms for handling exceptions: Catching Exceptions ( Try Catch finally ) declares an exception ( throws ) finally and Final,finalizer
Catching Exceptions:
Try Catch finally
catch Exception (try catch finally ) try { generates an exception code }catch (Someexception e) { // Span style= "color: #008000;" > exception handling } catch (SomeException1 e) {// exception handling finally { is there any exception Code to execute }
PackageCom.study4; Public classTrycatch { Public Static voidMain (string[] args) {System.out.println (Start); inti = 10; intj = 0; //Catching exceptions Try{ intK = i/J; }Catch(ArithmeticException e) {System.out.println (E.getmessage ());//Get exception informationE.printstacktrace ();//Printing Information} System.out.println (End); }}
Operation Result:
start /by Zero at com.study4.Trycatch.main (Trycatch.java:ten) end
Human handling exception: Throw
Public Static void mathod2 () { System.out.println ("program start"); int i = ten; int j = 0; if (j = = 0) {thrownew arithmeticexception ("Dividend cannot be 0"); Throw exception } System.out.println ("program End"); }
Operation Result:
"Main" java.lang.ArithmeticException: Dividend cannot be 0 at com.study4.Trycatch.mathod2 (trycatch.java:) At Com.study4.Trycatch.main (Trycatch.java:5)
Description
Throws declares an exception throw throws an exception if throw throws a compile exception. Then the method has to do the throws exception declaration. Explicitly tell other callers to handle this compilation exception.
The use of finally:
The code in the finally block executes, regardless of whether the program appears to be an exception, to the finally, which is typically used to do the resource shutdown operation. Finally executes with return first.
code example:
Public Static void mathod3 () { try{ class.forname ("Com.aa"); } Catch (ClassNotFoundException e) { System.out.println ("Com.aa class not Found"); E.printstacktrace (); } finally { // The Code System.out.println ("program continues to run") to be executed anyway ; } System.out.println ("End"); }
Operation Result:
could not find Com.aa class Java.lang.ClassNotFoundException:com.aa at Java.net.URLClassLoader.findClass ( URLClassLoader.java:381) at java.lang.ClassLoader.loadClass (classloader.java:424) at Sun.misc.launcher$appclassloader.loadclass (Launcher.java:331) at Java.lang.ClassLoader.loadClass ( Classloader.java:357) at JAVA.LANG.CLASS.FORNAME0 (Native Method) at Java.lang.Class.forName ( Class.java:at com.study4.Trycatch.mathod3 (Trycatch.java:+) at Com.study4.Trycatch.main (Trycatch.java:7) program continues to run end
Custom Exceptions:
Why custom Exceptions Java programmers can define more ways to handle exceptions custom exception classes can provide more complex and detailed methods, which can greatly enhance the robustness of software such as blexception: using an abnormal function number to find the exception information code more clearly how to customize the exception: The first way of defining, inheriting the second definition of the exception class: inheriting the Throwable class
Code:
Agetoobig.java
package com.study4.self; public class Agetoobig extends Exception { public Agetoobig (String message) { (message); public Agetoobig (String message, Throwable cause) { super (message, cause) ; public Agetoobig (Throwable throwable) { super (Throwable); }}
Agetoosmall.java
package com.study4.self; public class Agetoosmall extends throwable{ public Agetoosmall (String message) { (message); public Agetoosmall (String message, Throwable cause) { super (message, cause) ; public Agetoosmall (throwable Throwable) { super (Throwable); }}
Checkperson.java
Packagecom.study4.self; Public classCheckperson { Public Static voidCheckage (intAgethrowsagetoosmall{if(Age < 20){ Throw NewAgetoosmall ("Too Young"); }Else if(Age > 40){ Throw NewAgetoosmall ("Too old"); }Else{System.out.println ("A very old and very helpful"); } } }
Testmain.java
Package com.study4.self; Public class Testmain { publicstaticvoid main (string[] args) { int a =; Try { checkperson.checkage (a); Catch (Agetoosmall e) { e.printstacktrace (); }}}
Operation Result:
Com.study4.self.AgeTooSmall: Too old at Com.study4.self.CheckPerson.checkage (Checkperson.java:8) At Com.study4.self.TestMain.main (Testmain.java:7)
Note Points for using exceptions:
detect exception information in advance do not set try and catch for each of the possible exception statements a try block can be used with multiple catch to avoid always catch Exception, and to catch specific exceptions do not suppress, conceal exceptions. Throws an unhandled exception to the catchand tries to place the try catch in an out-of-loop catch exception, you must first catch the small exception, and then catch the large exception
"Learning Notes" Java exceptions