java中继承及异常处理!
2010-04-13 21:16
357 查看
实现图解:
下面是一个关于检验年龄是否合格的实例.用到继承及异常处理的相知识!
1~~~~~~~~~~~~~~~~~~~~~~
package age;
class AgreeException extends Exception {
int age;
AgreeException(int age, String s) {
super(s);
this.age = age;
}
AgreeException() {
super();
}
}
2~~~~~~~~~~~~~~~~~~~~~
class IllegalAgeException extends AgreeException {
IllegalAgeException(int age, String s) {
super(age, s);
}
IllegalAgeException() {
super();
}
}
3~~~~~~~~~~~~~~~~~~~~~~~
class OutlimitException extends AgreeException
{
int agelimit;
OutlimitException(int age,int agelimit,String s)
{
super(age,s);
this.agelimit=agelimit;
}
OutlimitException(){
super();
}
}
4~~~~~~~~~~~~~~~~~~~~~~~~
class NegativAagEexception extends IllegalAgeException {
NegativAagEexception(int age) {
super(age, "年龄不能为负数! ");
}
NegativAagEexception() {
super();
}
}
5~~~~~~~~~~~~~~~~~~~~~~~~
class OldException extends OutlimitException{
OldException(int age,int agelimit)
{
super(age,agelimit,"大于年龄限制");
}
OldException()
{
super();
}
}
6~~~~~~~~~~~~~~~~~~~~~~~
package age;
class YoungException extends OutlimitException{
YoungException(int age,int agelimit)
{
super(age,agelimit,"小于年龄限制");
}
YoungException()
{
super();
}
}
7~~~~~~~~~~~~~~~~~~~~~~~
public class Exceptiondemo {
static int lowlimit;
static int highlimit;
static void check(int age) throws NegativAagEexception, OutlimitException {
if (age < 0) {
throw new NegativAagEexception(age);
} else if (age < lowlimit) {
throw new YoungException(age, lowlimit);
} else if (age > highlimit) {
throw new OldException(age, highlimit);
}
System.out.println(age + " 年龄符合要求!");
}
public static void main(String[] args) {
int[] age = { -11, 16, 70, 12 };
highlimit = 60;
lowlimit = 15;
System.out.println("开始检测 :" + '/r' + age[0]);
for (int i = 0; i < age.length; i++) {
try {
check(age[i]);
} catch (NegativAagEexception e) {
System.out.println(e);
// e.printStackTrace();
} catch (OutlimitException e) {
System.out.println(e);
// e.printStackTrace();
} finally {
if (i < age.length - 1) {
System.out.println("待检测的年龄" + age[i + 1]+'/r');
}
}
}
}
}
下面是一个关于检验年龄是否合格的实例.用到继承及异常处理的相知识!
1~~~~~~~~~~~~~~~~~~~~~~
package age;
class AgreeException extends Exception {
int age;
AgreeException(int age, String s) {
super(s);
this.age = age;
}
AgreeException() {
super();
}
}
2~~~~~~~~~~~~~~~~~~~~~
class IllegalAgeException extends AgreeException {
IllegalAgeException(int age, String s) {
super(age, s);
}
IllegalAgeException() {
super();
}
}
3~~~~~~~~~~~~~~~~~~~~~~~
class OutlimitException extends AgreeException
{
int agelimit;
OutlimitException(int age,int agelimit,String s)
{
super(age,s);
this.agelimit=agelimit;
}
OutlimitException(){
super();
}
}
4~~~~~~~~~~~~~~~~~~~~~~~~
class NegativAagEexception extends IllegalAgeException {
NegativAagEexception(int age) {
super(age, "年龄不能为负数! ");
}
NegativAagEexception() {
super();
}
}
5~~~~~~~~~~~~~~~~~~~~~~~~
class OldException extends OutlimitException{
OldException(int age,int agelimit)
{
super(age,agelimit,"大于年龄限制");
}
OldException()
{
super();
}
}
6~~~~~~~~~~~~~~~~~~~~~~~
package age;
class YoungException extends OutlimitException{
YoungException(int age,int agelimit)
{
super(age,agelimit,"小于年龄限制");
}
YoungException()
{
super();
}
}
7~~~~~~~~~~~~~~~~~~~~~~~
public class Exceptiondemo {
static int lowlimit;
static int highlimit;
static void check(int age) throws NegativAagEexception, OutlimitException {
if (age < 0) {
throw new NegativAagEexception(age);
} else if (age < lowlimit) {
throw new YoungException(age, lowlimit);
} else if (age > highlimit) {
throw new OldException(age, highlimit);
}
System.out.println(age + " 年龄符合要求!");
}
public static void main(String[] args) {
int[] age = { -11, 16, 70, 12 };
highlimit = 60;
lowlimit = 15;
System.out.println("开始检测 :" + '/r' + age[0]);
for (int i = 0; i < age.length; i++) {
try {
check(age[i]);
} catch (NegativAagEexception e) {
System.out.println(e);
// e.printStackTrace();
} catch (OutlimitException e) {
System.out.println(e);
// e.printStackTrace();
} finally {
if (i < age.length - 1) {
System.out.println("待检测的年龄" + age[i + 1]+'/r');
}
}
}
}
}
相关文章推荐
- Java_常瑞鹏 java内部类,抽象类,继承,接口,异常的处理等
- Java继承Exception自定义异常类教程以及Javaweb中用Filter拦截并处理异常
- Java异常之异常继承体系和处理方式
- Java -- 异常的捕获及处理 -- 异常类的继承结构
- Java__内部类,抽象类,继承,接口,异常的处理...等
- Java自学笔记(第十天)-异常-异常的处理-throws-throw-多异常处理-自定义异常-throwable类-RunTimeException类-finally-异常在方法继承中的注意事项-
- Java__内部类,抽象类,继承,接口,异常的处理...等
- JAVA异常处理机制
- Java基础(十)异常处理
- java 异常处理
- Java 异常处理
- java之异常处理Exception的应用和自定义异常类的应用
- java异常处理总结
- 有效处理 Java 异常三原则
- Android 中 java.lang.IllegalArgumentException: pointerIndex out of range 异常的处理方法
- java的异常处理机制要点总结
- java异常处理Exception
- java异常处理的使用。
- Java总结第二篇 异常处理--基本概念
- Java 中的异常处理机制的简单原理和应用?