how to throw an IOException?

前端 未结 7 1767
天涯浪人
天涯浪人 2020-12-19 04:17
public class ThrowException {
    public static void main(String[] args) {
        try {
            foo();
        }
        catch(Exception e) {
             if (e         


        
7条回答
  •  渐次进展
    2020-12-19 05:20

    I just started learning exceptions and need to catch an exception

    To throw an exception

    throw new IOException("Something happened")
    

    To catch this exception is better not use Exception because is to much generic, instead, catch the specific exception that you know how to handle:

    try {
      //code that can generate exception...
    }catch( IOException io ) {
      // I know how to handle this...
    }
    

提交回复
热议问题