java实现自定义异常实例代码

此处主要便于对异常类的使用上方便大家理解以一个公约数为例做了一个简单自定义异常的处理代码如下:

如果操作者输入数字符合要求程序运行,不符合则抛出错误。

package 自定义异常简单实例;
import java.util.Scanner;
public class CommonDivisor {
	static Scanner in;
	public void gongyue(int m,int n) throws Exception{
		if(m<0||n<0) {
			throw new  Exception("所求数都不能为负数");
		}
		else {
			int p,o=0;
			if(m<n) {
				p=m;
			}
			else {
				p=n;
			}
			for(int i=1;i<=p;i++) {
				if(m%i==0&&n%i==0) {
					o=i;
				}
			}
			System.out.println("所输入的两个数的最大公约数为"+o);
		}
	}
	public static void main(String args[]){
		in=new Scanner(System.in);
		CommonDivisor a=new CommonDivisor();
		int p1,p2;
		System.out.println("请输入两个数系统会求输入数的公约数");
		p1=in.nextInt();
		p2=in.nextInt();
		try {
		  a.gongyue(p1, p2);
		} catch(Exception e) {
		  e.printStackTrace();  
		}
	}

}

异常类中方法可以直接调用——直接定义对象直接使用方法。

异常类使用时也可以自定义一个异常类直接继承Exception类代码如下

public class MyException extends Exception {			
	public MyException(String ErrorMessagr) {		
		super(ErrorMessagr);					
	}
}    //主函数中定义异常语句时可直接使用MyException操作

package com.hexiang.utils; import java.awt.Component; import javax.swing.JOptionPane; /** * This class ExceptionManager and its subclasses are a form of * Exception. It is used to wrap all the Throwable instances * and handle them in a unified way. It will show the information which consists of * StackTraces and Messages by using JOptionPanel. * * @author Estelle * @version 1.0 * @see java.lang.Exception * @since jdk 1.5 */ public class ExceptionManager extends RuntimeException { private static final long serialVersionUID = -6963187366089365790L; /** * This field alerter is used to show the information the Class offered. * * @see javax.swing.JOptionPane */ private JOptionPane alerter; /** * This static method create an instance of the ExceptionManager by invoking the * constructor ExceptionManager(String msg). * * @param msg The message will pass the specified constructor * @return An instance of the ExceptionManager created by invoking the constructor * ExceptionManager(String msg). */ public static ExceptionManager wrap(String msg){ return new ExceptionManager(msg); } /** * This static method create an instance of the ExceptionManager by invoking the * constructor ExceptionManager(Throwable throwable). * * @param throwable The cause will pass the specified constructor * @return An instance of the ExceptionManager created by invoking the constructor * ExceptionManager(Throwable throwable). */ public static ExceptionManager wrap(Throwable throwable){ return new ExceptionManager(throwable); } /** * This static method create an instance of the ExceptionManager by invoking the * constructor ExceptionManager(String msg,Throwable throwable). * * @param msg The message will pass the specified constructor * @param throwable The cause will pass the specified c
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值