Used to handle specific problems that may occur in your project, and to customize the exception encapsulation for these issues.
Exceptiontest.java
Public classExceptiontest { Public Static voidMain (string[] args) {Divdemo dd=NewDivdemo (); intResultd; Try{Resultd= Dd.div (10,-1); System.out.println ("Resultd ' value is:" +Resultd); } Catch(ArithmeticException e) {System.out.println ("Arithmetic exception:" +e.tostring ()); } Catch(fushuexception e) {System.out.println ("Fushu Exception:" +e.tostring ()); System.out.println ("Negative number is:" +e.value); } System.out.println ("Over!"); }}//Create a custom exception class---Inherit exception, it is possible to parabolic, this parabolic is Throwable system has. classFushuexceptionextendsexception{ intvalue; Fushuexception () {Super(); } //Exception Message MessageFushuexception (String msg,intval) { Super(msg); This. Value =Val; } Public intGetValue () {return This. Value; }}classdivdemo{ Public intDivintAintbthrowsarithmeticexception,fushuexception{if(A < 0 ){ Throw NewFushuexception ("a negative number has occurred", a); //Active throw fushuexception anomaly } if(b < 0 ){ Throw NewFushuexception ("a negative number has occurred", B); //Active throw fushuexception anomaly } returnA/b; }}
<console>
Java Basics-Exceptions-Custom exceptions