Exception Handling Generics Amit Gupta
Announcements Project 2 deadline 18 th Feb 9 pm. TA Consulting hours Mon Thurs B146 6 9 pm Exam 1 : Feb 15 4:30 5:20 pm Project 1 grading
Exception Handling Computer programs are filled with errors. It may call other code which contain mistakes. We have to handle program errors. What mechanisms Java provides in order to deal with them?
Exception Handling You call a method in a class that you did not write That method does some risky, something that might not work at runtime. You should know that the method you are calling is RISKY.
Exception Handling Methods in Java use exceptions to tell the calling code that something bad happened. You then write code that can handle the failure if it does happen.
How to define a RISKY method throws keyword in Java declares that a method throws an exception. An exception is always thrown back to the caller.
Example: Java Documentation
Handling Exceptions One method will catch what another method throws. Calling Risky method code should be within try/catch block. Code for handling an exception is written within catch block.
What is an Exception?? An exception is an object of class Exception. All Exception classes extend Throwable and inherit two methods. getmessage(); getstacktrace();
Checked and Unchecked Exceptions Java Compiler checks for everything except Runtime Exception(Unchecked Exceptions). Most Runtime exceptions come from a problem in your code logic that fails at runtime in ways you cannot predict. You WANT Runtime Exceptions to happen at development time.
Flow Control in try/catch blocks If try succeeds If try fails
finally keyword A finally block is where you put code that must run regardless of an exception. Without finally, you have to put code in both the try and catch blocks.
Multiple Exceptions A method can throw multiple exceptions. It should define all the checked exceptions it can throw. The calling code must handle all the exceptions thrown by the method.
Polymorphic Exceptions Exceptions can be declared using a super type of the exceptions. It can catch any exception of Clothing Exception subclass. You can catch specific type of exceptions and handle each of them or catch a super type and handle them in a same way
Order of Multiple Catch Blocks Multiple catch blocks must be ordered from smallest to biggest. DON T DO THIS With catch blocks, JVM starts at first one and goes down until it finds a catch block that s broad enough.
Ducking an exception When you don t want to handle an exception, you can duck it by declaring it. Ducking(by declaring) only delays the inevitable. Sooner or later, somebody has to deal with it. If exception reaches main() and it also ducks it, JVM shuts down.
Handle or Declare. It s the LAW Handle Declare
Summary : Exception Handling Whenever an unusual situation is reached, an exception is thrown and normal execution stops immediately. The exception looks for code that is designed to deal with that specific exception. The code which handles exception can be in current method, in the calling method, or far back in a chain of method calls, all the way to main().
Summary : Exception Handling If no handling code is found, the exception will propagate all the way past main() to the JVM and the program will end. Exceptions provide a simple and unified way to handle errors. You can choose to deal with errors directly or delegate that responsibility to the methods that call the code you are writing.
Questions
Generics A generic class gives a template for creating classes in which a placeholder for the underlying data type can be filled in when a specific instance of that class is created. Same definition of MyGeneric is used with different underlying data types. If we omit the underlying type, the compiler will use Object as the underlying data type.
With Generics, we can create type safe collections where more problems are collected at compile time instead of runtime. Without generics, the compiler would let you put any object to an ArrayList that was supposed to hold Fish objects. Why Generics??
1. Creating instances of generic classes. 2. Declaring and assigning variables of generic classes. 3. Declaring and Invoking methods that take generic types. Using Generics
Defining Generic classes Defines a generic version of a linked list. T is used as a placeholder for different data types.
Questions
NO QUIZ TODAY
ALL THE BEST!