class FuShuException extends Exception
{
private int value;
//private String message;
FuShuException(String message,int value)
{
super(message);
this.value = value;
}
/*
public String getMessage()
{
return message;
}
*/
public int getValue()
{
return value;
}
}
class Demo
{
int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException,FuShuException//在功能上通过throws的关键字声明了该功能有可能会出现问题。
{
if (b<0)
throw new FuShuException("除数为负数了",b);//手动抛出一个自定义异常 return a/b;
}
}
运行结果:FuShuException无法向Throwable转型

解决方案 »

  1.   

    package adadf;import java.io.*;
    import java.util.Arrays;class FuShuException extends Exception
    {
    private int value;
    //private String message;
    FuShuException(String message,int value)
    {
    super(message);
    this.value = value;
    }
    /*
    public String getMessage()
    {
    return message;
    }
    */
    public int getValue()
    {
    return value;
    }
    }public class Main
    {
    int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException,FuShuException//在功能上通过throws的关键字声明了该功能有可能会出现问题。
    {
    if (b<0)
    throw new FuShuException("除数为负数了",b);//手动抛出一个自定义异常

    return a/b;
    }

    public static void main(String[] args) {
    Main tmp = new Main();
    try {
    tmp.div(3,-2);
    } catch (ArrayIndexOutOfBoundsException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ArithmeticException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (FuShuException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    可运行
      

  2.   

    中午运行的时候老是不行,老是显示FuShuException无法转型为Throwable,刚试了一下又好了,真的奇怪,我明明已经继承了Exception类了
      

  3.   

    class LanPingException extends Exception
    {
    LanPingException(String message)
    {
    super(message);
    }
    }
    class MaoYanException extends Exception
    {
    MaoYanException(String message)
    {
    super(message);
    }
    }
    class NoPlanException extends Exception
    {
    NoPlanException(String message)
    {
    super(message);
    }}
    class Computer
    {
    private int state = 1;
    public void run() throws LanPingException
    {
    if (state == 2)
    throw new LanPingException("电脑蓝屏");
    else if (state == 3)
    throw new MaoYanException("电脑冒烟");
    System.out.println("电脑运行");
    }
    public void reset() 
    {
    state = 1;
    System.out.println("重启");
    }
    }
    class Teacher
    {
    private String name;
    private Computer c;
    Teacher(String name)
    {
    this.name = name;
    c = new Computer();
    }
    public void lecture() throws NoPlanException

    try
    {
    c.run();
    }
    catch (LanPingException e)
    {
    c.reset();
    }
    catch (MaoYanException e)

    test();
    throw new NoPlanException("电脑冒烟无法上课"+e.getMessage());
    }
    System.out.println("讲课");

    }
    public void test()
    {
    System.out.println("练习");
    }
    }
    class ExceptionTest 
    {
    public static void main(String[] args) 
    {
    Teacher t = new Teacher("Bi");
    try
    {
    t.lecture();
    }
    catch (NoPlanException e)
    {
    System.out.println(e.toString());
    System.out.println("换老师或者放假");
    }

    }
    }
      

  4.   


    public void run() throws LanPingException,MaoYanException{
    if (state == 2)
    throw new LanPingException("电脑蓝屏");
    else if (state == 3)
    throw new MaoYanException("电脑冒烟");
    System.out.println("电脑运行");
    }//public void run() throws LanPingException,MaoYanException{
    你没有将MaoYanException从方法中抛出(因为你方法内部也没有对此异常进行处理)