1. Throws 用来声明一个Exception,Throw是关键子抛出一个具体的实例Exception
2. 从语法上说Throws后面是类名,Throw后面是对象
3. Throws用在方法声明处也就是方法名后面,Throw用在方法体里
- ....
- static{
- try {
- throw new Exception("Something went wrong!!");
- } catch (Exception exp) {
- System.out.println("Error: "+exp.getMessage());
- }
- }
- ....
复制代码- public void sample() throws ArithmeticException{
- //Statements
- .....
- //if (Condition : There is an error)
- ArithmeticException exp = new ArithmeticException();
- throw exp;
- ...
- }
复制代码
4. Throws可以抛出多个Exception,Throw一次只能抛一个
- throw new ArithmeticException("An integer should not be divided by zero!!")
- throw new IOException("Connection failed!!")
复制代码- throws IOException, ArithmeticException, NullPointerException,
- ArrayIndexOutOfBoundsException
复制代码
|