自己抛出异常,调用者捕获异常,
class DefineException
{
public int shang(int x,int y)
{ int m=0;
try{
m=x/y;
throw new DataParseException("11111111");
}catch(DataParseException e){
e.printStackTrace();
}
return m;
}
}
上面会执行 e.printStackTrace()语句,同时也不能缺少 throw new DataParseException("11111111"),但是即有这话它也不会打印11111111,如果没有throw new DataParseException("111111111")这一句将会出现Unreachable catch block for DataParseException. This exception is never thrown from the try statement body 也说是说你这执行语块中根本没有抛出任何异常,不可能有捕获异常的语句,
class Numbertest
{
public int shang(int x,int y) throws DataParseException
{
int m=0;
if(m==1)
{
System.out.println("finally!") ;
}
else{
throw new DataParseException();
}
return m;
}
}
或是这样
class Numbertest
{
public int shang(int x,int y)
{
int m=0;
try{
m=6;
}catch(DataParseException e){
throw new DataParseException();
}
return m;
}
}
调用的时候: 这样即可,不需要在调用处的这个方法后面加上throws DataParseExcepton
public ovid pumethod{
Numbertest n=new Numbertest();
try {
n.shang(2, -3);
} catch (DataParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
上面如果没有throws DataParseException 会报错,我也不知道原因,还请高人指教
最近在项目中需用到自定义异常,在网上粗略的查了一下这类的信息,其中有很多博客给我们讲解了此类的文章 :
http://www.189works.com/article-42157-1.html
自定义异常 : 步骤:
一:创建一个类继承于Throwable或其子类;
二:添加构造方法;
三:在一个方法中使用throw抛出异常
四:在另一个方法中捕获并处理异常
示例:
一、继承Exception类
- public class MyFirstException extends Exception {
- public MyFirstException() {
- super();
- }
- public MyFirstException(String msg) {
- super(msg);
- }
- public MyFirstException(String msg, Throwable cause) {
- super(msg, cause);
- }
- public MyFirstException(Throwable cause) {
- super(cause);
- }
- //自定义异常类的主要作用是区分异常发生的位置,当用户遇到异常时,
- //根据异常名就可以知道哪里有异常,根据异常提示信息进行修改。
- }
二、 继承Throwable 类
- public class MySecondException extends Throwable {
- public MySecondException() {
- super();
- }
- public MySecondException(String msg) {
- super(msg);
- }
- public MySecondException(String msg, Throwable cause) {
- super(msg, cause);
- }
- public MySecondException(Throwable cause) {
- super(cause);
- }
- }
三、测试
- /**
- * 自定义异常类的使用
- * @author new
- *
- */
- public class TestMyException {
- public static void firstException() throws MyFirstException{
- throw new MyFirstException("\"firstException()\" method occurs an exception!");
- }
- public static void secondException() throws MySecondException{
- throw new MySecondException("\"secondException()\" method occurs an exception!");
- }
- public static void main(String[] args) {
- try {
- TestMyException.firstException();
- TestMyException.secondException();
- } catch (MyFirstException e1){
- System.out.println("Exception: " + e1.getMessage());
- e1.printStackTrace();
- } catch (MySecondException e2){
- System.out.println("Exception: " + e2.getMessage());
- e2.printStackTrace();
- }
- //当一个try块后面跟着多个catch块时,如果发生的异常匹配第一个catch块的参数,便将异常处理权利交给第一个catch块。
- //如果发生的异常与第一个catch块不匹配,便看是否与第二个catch块匹配,依次下去,如果到最后依然无法匹配该异常,
- //便需要在方法声明中添加一条throw语句,将该异常抛出。
- //因此,在有多个catch块,而且每次处理的异常类型具有继承关系时,应该首先catch子类异常,再catch父类异常。
- //比如,如果MySecondException继承MyFirstException,那么最好将catch(MySecondException e2)放在前面
- //把catch (MyFirstException e1)放在后面。
- }
/*下面做了归纳总结,欢迎批评指正*/
/*自定义异常*/
class ChushulingException extends Exception
{
public ChushulingException(String msg)
{
super(msg);
}
}
class ChushufuException extends Exception
{
public ChushufuException(String msg)
{
super(msg);
}
}
/*自定义异常 End*/
class Numbertest
{
public int shang(int x,int y) throws ChushulingException,ChushufuException
{
if(y<0)
{
throw new ChushufuException("您输入的是"+y+",规定除数不能为负数!");//抛出异常
}
if(y==0)
{
throw new ChushulingException("您输入的是"+y+",除数不能为0!");
}
int m=x/y;
return m;
}
}
class Rt001
{
public static void main(String[]args)
{
Numbertest n=new Numbertest();
//捕获异常
try
{
System.out.println("商="+n.shang(1,-3));
}
catch(ChushulingException yc)
{
System.out.println(yc.getMessage());
yc.printStackTrace();
}
catch(ChushufuException yx)
{
System.out.println(yx.getMessage());
yx.printStackTrace();
}
catch(Exception y)
{
System.out.println(y.getMessage());
y.printStackTrace();
}
finally{ System.out.println("finally!");} finally不管发没发生异常都会被执行
}
}
/*
[总结]
1.自定义异常:
class 异常类名 extends Exception
{
public 异常类名(String msg)
{
super(msg);
}
}
2.标识可能抛出的异常:
throws 异常类名1,异常类名2
3.捕获异常:
try{}
catch(异常类名 y){}
catch(异常类名 y){}
4.方法解释
getMessage() //输出异常的信息
printStackTrace() //输出导致异常更为详细的信息
*/
对java自定义异常还不是很了解的朋友,这两篇文章值得我们去学习,