在文章“Unreachable catch block for IOException. This exception is never thrown from the try statement body”中有小伙伴提到:我的是一个自定义的异常,用到自定义的异常就出现这个问题,请问怎么解决?这里讲一下如何使用自定义类型的异常,以及为什么要使用自定义类型的异常。
其实这是问题处理的模式/方式之一;
其它的是什么?例如WindowsAPI的GetLastError,swift、Go的多返回值 ,返回一个实体类包含所有需要的信息;
而这个方法同样也有多种应用。
应用一:你的一个方法中把一些必须满足的条件作为异常来处理;
应用二:你的一个方法中把所有可能遇到的异常都捕获了,附加上其它信息抛出新的异常;
应用三:以上两种混合应用;
自定义异常类:CustomException.java
package wasdev.sample.servlet;
/**
* 自定义异常
* @author testcs_dn
*
*/
public class CustomException extends Exception {
private static final long serialVersionUID = -3260180827302646043L;
/**
* 扩展信息
*/
private String exMsg;
public String getExMsg() {
return exMsg;
}
public void setExMsg(String exMsg) {
this.exMsg = exMsg;
}
/**
*
*/
public CustomException() {
super();
}
public CustomException(String msg) {
super(msg);
}
/**
*
* @param msg 异常信息
* @param exMsg 扩展信息
*/
public CustomException(String msg, String exMsg) {
super(msg);
this.setExMsg(exMsg);
}
}
应用类Servlet:SimpleServlet.java
package wasdev.sample.servlet;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SimpleServlet
*/
@WebServlet("/SimpleServlet")
public class SimpleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.getWriter().print("Hello World!");
try {
doSomething();
} catch (CustomException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
doSomething("user", "pwd");
} catch (CustomException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doSomething() throws CustomException{
throw new CustomException("abcd");
}
protected void doSomething(String userName, String password) throws CustomException{
if (userName == null || userName.length() == 0){
throw new CustomException("abcd", "userName");
}
if (password == null || password.length() == 0){
throw new CustomException("abcd", "password");
}
File f = new File("test.txt");
if (f.exists()){
try {
FileOutputStream fos = new FileOutputStream(f);
try {
fos.close();
} catch (IOException e) {
throw new CustomException("abcd", "IOException");
}
} catch (FileNotFoundException e) {
throw new CustomException("abcd", "FileNotFoundException");
}
}
}
protected void doSomething2(){
try {
throw new CustomException("abcd", "扩展信息");
} catch (CustomException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
如果你有更好的应用场景欢迎分享!
我这里只定义了一个异常类:CustomException,你也可以做如下定义,只要你喜欢:
UserNameException,PasswordException,FileWriteException……等等。
===========文档信息============
版权声明:非商用自由转载-保持署名-注明出处
署名(BY) :testcs_dn(微wx笑)
文章出处:[无知人生,记录点滴](http://blog.csdn.net/testcs_dn)