Java,父类,带有异常抛出的方法

Java,父类,带有异常抛出的方法,java,exception,Java,Exception,我有一个方法,如何抛出异常。而不是试图抓住 这是一个基本的void方法,读取文件 public void method(String filename){ //does some stuff to the file here } 简单到: public void method(String filename) throws Exception { if (error) throw new Exception("uh oh!"); } 或者,如果需要自定义异常: cla

我有一个方法,如何抛出异常。而不是试图抓住

这是一个基本的void方法,读取文件

public void method(String filename){
//does some stuff to the file here
}
简单到:

public void method(String filename) throws Exception
{
    if (error)
        throw new Exception("uh oh!");
}
或者,如果需要自定义异常:

class MyException extends Exception
{
    public MyException(String reason)
    {
        super(reason);
    }
}

public void method(String filename) throws MyException
{
    if (error)
        throw new MyException("uh oh!");
}
简单到:

public void method(String filename) throws Exception
{
    if (error)
        throw new Exception("uh oh!");
}
或者,如果需要自定义异常:

class MyException extends Exception
{
    public MyException(String reason)
    {
        super(reason);
    }
}

public void method(String filename) throws MyException
{
    if (error)
        throw new MyException("uh oh!");
}

作为第一步,我认为你需要通过

这取决于要抛出的异常类型

如果要抛出未检查的异常

public void method(String filename){
    if(error condition){
        throw new RuntimeException(""); //Or any subclass of RuntimeException
    }
}
public void method(String filename) throws Exception{ //Here you can mention the exact type of Exception thrown like IOExcption, FileNotFoundException or a CustomException
    if(error condition){
        throw new Exception(""); //Or any subclass of Exception - Subclasses of RuntimeException
    }
}
如果要抛出选中的异常

public void method(String filename){
    if(error condition){
        throw new RuntimeException(""); //Or any subclass of RuntimeException
    }
}
public void method(String filename) throws Exception{ //Here you can mention the exact type of Exception thrown like IOExcption, FileNotFoundException or a CustomException
    if(error condition){
        throw new Exception(""); //Or any subclass of Exception - Subclasses of RuntimeException
    }
}

作为第一步,我认为你需要通过

这取决于要抛出的异常类型

如果要抛出未检查的异常

public void method(String filename){
    if(error condition){
        throw new RuntimeException(""); //Or any subclass of RuntimeException
    }
}
public void method(String filename) throws Exception{ //Here you can mention the exact type of Exception thrown like IOExcption, FileNotFoundException or a CustomException
    if(error condition){
        throw new Exception(""); //Or any subclass of Exception - Subclasses of RuntimeException
    }
}
如果要抛出选中的异常

public void method(String filename){
    if(error condition){
        throw new RuntimeException(""); //Or any subclass of RuntimeException
    }
}
public void method(String filename) throws Exception{ //Here you can mention the exact type of Exception thrown like IOExcption, FileNotFoundException or a CustomException
    if(error condition){
        throw new Exception(""); //Or any subclass of Exception - Subclasses of RuntimeException
    }
}

如果异常是未检查的异常,则不需要
抛出
。此外,将带参数的构造函数添加到
MyException
。如果异常是未检查的异常,则不需要
抛出
。此外,将带参数的构造函数添加到
MyException