Class Notes (835,754)
Canada (509,376)
CSC207H1 (40)
Lecture

nov21c.docx

3 Pages
156 Views
Unlock Document

Department
Computer Science
Course
CSC207H1
Professor
Diane Horton
Semester
Fall

Description
NOVEMBER 21 Exceptions  Recap o If you call code that may throw an exception, you have two choices. o When you declare that a method “throws” something, you are reserving the right to do so, not guaranteeing that you will. o Exceptions don’t follow the normal control flow. o Some guidelines on using exceptions well:  Use exceptions for exceptional circumstances.  Throwing and catching should not be in the same method. “Throw low, catch high”. o Benefits of using exceptions?  Where Exception fits in  “Throwable” has useful methods o Constructors: Throwable(), Throwable(String message) o Other Methods: getMessage() printStackTrace() getStackTrace() o You can also record (and look up) within a Throwable it’s “cause”: another Throwable that caused it to be thrown.  Through this, you can record (and look up) a chain of exceptions.  Don’t have to handle Errors or RuntimeExceptions o Error  ”Indicates serious problems that a reasonable application should not try to catch.”  Do not have to handle these errors because they “are abnormal conditions that should never occur.” o RuntimeException  These are called “unchecked” because you do not have to handle them  A good thing, because so many methods throw them it would be cumbersome to check them all  Some things not to catch o Don’t catch Error: You can’t be expected to handle these. o Don’t catch Throwable or Exception: Catch something more specific. o (You can certainly do so when you’re experimenting with exceptions. Just don’t do it in real code without a good reason.)  What should you throw? o You can throw an instance of Throwable or any subclass of it (whether an already defined subclass, or a subclass you define). o Don’t throw an instance of Error or any subclass of it: These are for unrecoverable circumstances. o Don’t throw an instance of Exception: Throw something more specific. o It’s okay to throw instances of:  Specific subclasses of Exception that are already defined  e.x., UnsupportedOperationException  Specific subclasses of Exception that you define. NOVEMBER 21  Extending Exception o Version 1: a method m() that throw your own exception MyException, a subclass of Exception 1. class MyException extends Exception { 2. ... 3. } 4. 5. class MyClass { 6. public void m() throws MyException { ... 7. if (...) { 8. throw new MyException("oops!"); 9. } 10. } 11. } o Version 2: a method m() that throw your own exception MyException, a subclass of Exception  has the exception class inside the only class which can throw it 1. class MyClass { 2. class MyException extends Exception { 3. ... 4. } 5. 6. public void m() throws MyException { ... 7. if (...) { 8. throw new MyException("oops!"); 9. } 10. } 11. }  Aside: classes inside other classes o You can define a class inside another class. these are two kinds  Static nested classes  use the static keyword  it can only be used with classes that are nested  cannot access any other members of the enclosing class  Inner classes  do not use the static keyword  can access all members of the enclosing class (even private ones) o Nested classes increases encapsulation  they make sense if you won’t need to use the class outside its enclosing class  Documenting Exceptions
More Less

Related notes for CSC207H1

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit