自定义异常的简单练习

自定义一个文件上传类型的异常

定义一个对所要进行上传的文件对象进行校验的类来实现对文件的格式进行校验操作

/**
 * 
 * @author Administrator
 *自定义一个异常类,该异常为文件上传出错的异常
 *在自定义类对象当中设定两个构造函数来实现对出现的异常信息进行抛出操作
 */
public class FileUpException extends Exception{
	FileUpException() {
		super();
	}
	FileUpException(String message)
	{
		super(message);
	}
}

文件校验类

/**
 * 
 * @author Administrator
 *当前类用于模拟实现一个文件对象的上传校验操作
 */
public class FileUp {
	public static void main(String[] args) throws Exception {
		new FileUp().upload("bai.xls","d:/desktop");
	}
	
	public void upload(String fileName,String path) throws Exception
	{
		if(fileName!=null && !fileName.trim().equals(""))
		{
			if(path!=null && !path.trim().equals(""))
			{
//				对所要进行上传文件对象的格式进行校验判断
				boolean b=fileName.endsWith(".xls");
				if(!b)
				{
					throw new FileUpException("文件的类型必须是.xls");
				}
				else
				{
//					在此处执行具体的进行文件上传的操作
					System.out.println("文件上传成功");
				}
			}
			else
			{
				throw new FileUpException("文件的路径不能够为空");
			}
			
		}
		else
		{
			throw new FileUpException("文件名不能够为空");
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_34970891/article/details/80682617