package com.what21.base16;
public class TestException extends Exception {
private static final long serialVersionUID = 2039023617341817264L;
public TestException() {
}
public TestException(String msg) {
super(msg);
}
public TestException(Throwable target) {
super(target);
}
public TestException(String msg, Throwable target) {
super(msg, target);
}
}
package com.what21.base16;
/**
* Java异常应用
*/
public class ExceptionTest {
/**
* @param args
*/
public static void main(String[] args) {
// 处理方法一
if(isNull("")){
System.out.println("字符串为空");
}
// 处理方法二
try{
checkNull("");
}catch(RuntimeException e){
System.out.println("字符串为空, " + e.getMessage());
}
try {
checkNull2("");
} catch (Exception e) {
System.out.println("字符串为空, 错误代码: " + e.getMessage());
}
try {
checkNull3(null);
} catch (TestException e) {
System.out.println("字符串为空, 错误代码: " + e.getMessage());
}
}
/**
* @param str
* @return
*/
private static boolean isNull(String str){
if(str==null){
return true;
}
if("".equals(str)){
return true;
}
return false;
}
/**
* @param str
*/
private static void checkNull(String str){
if(str==null){
throw new RuntimeException("param is null");
}
if("".equals(str)){
throw new RuntimeException("param is empty");
}
}
/**
* @param str
* @throws Exception
*/
private static void checkNull2(String str)throws Exception{
if(str==null){
throw new Exception("01");
}
if("".equals(str)){
throw new Exception("02");
}
}
/**
* @param str
* @throws TestException
*/
private static void checkNull3(String str)throws TestException{
if(str==null){
throw new TestException("01");
}
if("".equals(str)){
throw new TestException("02");
}
}
}