In this program, the divisor is-1, also considered to be wrong, is unable to perform the operation.
Then you need to have a custom description of the problem.
When a throw throws an exception object inside the function, the corresponding processing action needs to be given.
Either in internal try catch processing, or on the function to declare the caller to handle.
In general, there is an exception in the function that needs to be declared on the function.
How do I quickly define exception information?
Because the operation of the exception information has been completed in the parent class.
So the subclass passes the exception information to the parent class as soon as it is constructed, through the super statement.
Then you can get the custom exception information directly through the GetMessage method.
Custom Exceptions:
Must be a custom class to inherit exception.
Inheritance exception Reason:
The exception system has one feature: because both the exception class and the exception object are thrown.
They all have a parabolic nature, which is unique to the throwable system.
Only classes and objects in this system can be throws and throw operations.
The difference between throw and throws:
(1) throws is used on the function.
Throw is used inside the function.
(2) throws followed by the exception class, you can follow a number of exception classes, exception classes separated by commas.
The throw is followed by an exception object.
Class Fushuexception extends Exception{/*private String msg; Fushuexception (String msg) {this.msg=msg;} Public String GetMessage () {return msg;} */int num; Fushuexception () {super ();//constructor for null argument. }fushuexception (String name,int num) {super (name);//Pass the exception information to the parent class through the Super statement this.num=num;} public void Show () {System.out.println ("Value of divisor:" +num);}} Class Demo{public int function (int a,int b) throws Fushuexception//This function may have an exception, a function that calls the function needs to throw an exception (throws) or to handle the exception (try ... catch). {int[] arr=new int[a];if (b<0) {throw new Fushuexception ("the divisor appears less than 0", b);//If the condition is met, the exception information is thrown. }return A/b;}} Class Exceptionclassdemo1{public static void Main (String args[])//throws fushuexception--This is throwing an exception, that is, compile without error, at run time the virtual opportunity prompt is different Information is often used. {Demo De=new demo ();//The following code handles the exception. Try{int result=de.function (5,-1); SYSTEM.OUT.PRINTLN (result);} catch (Fushuexception e) {System.out.println (E.getmessage ()); E.show ();} /*int result=de.function (5,-1); SYSTEM.OUT.PRINTLN (result); */}}
Java Exception Two