交通大学资讯工程学系

上传人:j****9 文档编号:60239661 上传时间:2018-11-14 格式:PPT 页数:80 大小:622.50KB
返回 下载 相关 举报
交通大学资讯工程学系_第1页
第1页 / 共80页
交通大学资讯工程学系_第2页
第2页 / 共80页
交通大学资讯工程学系_第3页
第3页 / 共80页
交通大学资讯工程学系_第4页
第4页 / 共80页
交通大学资讯工程学系_第5页
第5页 / 共80页
点击查看更多>>
资源描述

《交通大学资讯工程学系》由会员分享,可在线阅读,更多相关《交通大学资讯工程学系(80页珍藏版)》请在金锄头文库上搜索。

1、Programming in Java,Exception handling, More examples, 蔡文能 交通大學資訊工程學系 tsaiwncsie.nctu.edu.tw,http:/www.csie.nctu.edu.tw/tsaiwn/java/,Agenda,Exception handling Computing factorial Loop method Recursive method Computing primes Sieve method Sorting an array Selection Sort Insertion Sort Bubble Sort Qui

2、ck Sort Merge Sort,Exception example,try readFromFile(“datafile“); catch (FileNotFoundException e) System.err.println(“Error: File not found“); ,Exception Handling: Some Options,Print something Throw a new exception Re-Throw the exception Fix the problem Exit,Exception Handling: Printing,You can pri

3、nt a stack trace by calling the exception method printStackTrace() Sometimes its better to send error messages to stderr: System.err.println(“Error: invalid thingy“); Some applications log error messages file logging service (syslog).,Exception Handling: throw,You can throw an exception from an exce

4、ption handler (a catch block). Allows you to change exception type and/or error message. You can also alter the base of the stack trace fillInStackTrace(),Exception example,public class ThrowUp public static void main(String args) String tmp; try / generate an ArrayIndexOutOfBoundsExceptions / (on p

5、urpose!). for (int i=0 ; iargs.length+10 ; i+) tmp = argsi; System.out.println(“I made it!“); catch (ArrayIndexOutOfBoundsException up) throw new SecurityException(“Trouble“); / main ,Exception Handling: Re-throw,You can throw an exception from an exception handler (a catch block) without changing a

6、nything: called rethrowing The caller needs to deal with the exception. This also happens if you dont catch the exception! sometimes you need to take some action and then rethrow the exception.,Another way to re-throw,You can allow selected types of exceptions to be propogated to the caller of your

7、method: void blah() throws IOException Within blah() you dont need to catch these exceptions (to be able to compile).,Exception Handling: Fix the problem.,You cant fix things and then resume execution automatically you can do this in C+. You can have a loop the retries the code again.,Exception Hand

8、ling: exiting,Sometimes the error is fatal, and you want to stop the program immediately. System.exit();,Wait.java,public class Wait public static void main(String args) boolean done=false; int seconds=0; try seconds = Integer.parseInt(args0); catch (Exception e) System.out.println(“Please specify t

9、he number of seconds“); System.exit(1); long initialtime = System.currentTimeMillis( ); while (!done) try if (System.currentTimeMillis()-initialtime seconds*1000) throw new Exception(“Not Yet - keep trying“); done=true; catch (Exception e) System.out.println(e); System.out.println(“Done!n“); ,How/wh

10、en do you generate exceptions?,Use throw: throw new Exception(“broken!“); You can use throw anywhere. you detect some error that means the following code should not be executed. In some cases, you can think of throw as a alternate return,Exception Enforcement,In general, you do the following: specif

11、y what exceptions each method can generate. write code to catch all exceptions that can be generated by a method call. The compiler (usually) enforces this it is a compilation error to call a method without catching its declared exception types.,RunTime Exceptions,There are exceptions that are gener

12、ated by the system (that are usually caused by programming mistakes): NullPointerException (null references) ArrayIndexOutOfBoundsException If you dont catch these, a stack trace will be generated and the program will terminate. The compiler does not force you to catch these exceptions.,Exception Ty

13、pes,Exceptions are objects! Exception types are classes. A (quite large!) hierarchy of classes. All exception types are derived from the class Exception there are some methods defined in this base class.,NullPointer.java,import java.util.*; public class NullPointer public static void main(String arg

14、s) Date f = new Date(); f=null; PrintSomething2(f); / comment out to test PrintSomething PrintSomething(f); / no checks, if x is null runtime exception default behavior static void PrintSomething(Object x) System.out.println(x.getClass( ).getName(); / we explicitly check for runtime exception! stati

15、c void PrintSomething2(Object x) try System.out.println(x.getClass().getName(); catch (RuntimeException re) System.out.println(“Fatal Error!“); System.exit(1); / main ,Throwable,RunTimeException,NullPointerException,ArithmeticException,IOException,Exception Type Hierarchy (partial),EOFException,Virt

16、ualMachineError,Some Exception Methods,These are actually inherited from throwable printStackTrace() fillInStackTrack() getMessage(),Creating Your Own Exception Types,It is often useful to create your own type of exception. generally all you create is a name. you can get fancy and add new methods to your exception class(es).,User-defined Exception Type,class FooException

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号