Exception Exception An exception is an event which
![Exception Exception](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-1.jpg)
![Exception • An exception is an event, which occurs during the execution of a Exception • An exception is an event, which occurs during the execution of a](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-2.jpg)
![• Runtime errors occur while a program is running if the JVM detects • Runtime errors occur while a program is running if the JVM detects](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-3.jpg)
![Exception Handling Exception Handling](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-4.jpg)
![Exception Handling • Exception Handling is a mechanism to handle runtime errors such as Exception Handling • Exception Handling is a mechanism to handle runtime errors such as](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-5.jpg)
![The JVM firstly checks whether the exception is handled or not. If exception is The JVM firstly checks whether the exception is handled or not. If exception is](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-6.jpg)
![](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-7.jpg)
![Hierarchy of exception classes Un-checked exceptions Hierarchy of exception classes Un-checked exceptions](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-8.jpg)
![](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-9.jpg)
![Checked Exception • Checked Exceptions are those, that have to be either caught or Checked Exception • Checked Exceptions are those, that have to be either caught or](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-10.jpg)
![Unchecked Exception • Unchecked exceptions are those exceptions whose handling is not verified during Unchecked Exception • Unchecked exceptions are those exceptions whose handling is not verified during](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-11.jpg)
![Commonly used sub-classes of Exception • Arithmetic. Exception • Array. Index. Out. Of. Bounds. Commonly used sub-classes of Exception • Arithmetic. Exception • Array. Index. Out. Of. Bounds.](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-12.jpg)
![](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-13.jpg)
![](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-14.jpg)
![Types of exceptions Types of exceptions](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-15.jpg)
![Hierarchy of exception classes Un-checked exceptions Hierarchy of exception classes Un-checked exceptions](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-16.jpg)
![Classification The exception classes can be classified into three major types: 1) system errors Classification The exception classes can be classified into three major types: 1) system errors](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-17.jpg)
![System errors • System errors are thrown by the JVM and are represented in System errors • System errors are thrown by the JVM and are represented in](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-18.jpg)
![Exceptions • Exceptions are represented in the Exception class, which describes errors caused by Exceptions • Exceptions are represented in the Exception class, which describes errors caused by](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-19.jpg)
![Runtime exceptions • Runtime exceptions are represented in the Runtime. Exception class, which describes Runtime exceptions • Runtime exceptions are represented in the Runtime. Exception class, which describes](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-20.jpg)
![](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-21.jpg)
![or with both. or with both.](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-22.jpg)
![It contains statements that are to be executed when the exception represented by the It contains statements that are to be executed when the exception represented by the](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-23.jpg)
![](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-24.jpg)
![Defining Generalized Exception Handler • A generalized exception handler is one that can handle Defining Generalized Exception Handler • A generalized exception handler is one that can handle](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-25.jpg)
![Key points • At a time only one Exception is occurs and at a Key points • At a time only one Exception is occurs and at a](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-26.jpg)
![Nested Try’s class Nested. Try. Demo { public static void main(String args[]){ try { Nested Try’s class Nested. Try. Demo { public static void main(String args[]){ try {](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-27.jpg)
![Java “throw” keyword • It is used for explicit exception throwing. throw <Exception-object>; • Java “throw” keyword • It is used for explicit exception throwing. throw <Exception-object>; •](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-28.jpg)
![Example (throwing exception) Example (throwing exception)](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-29.jpg)
![Example (throwing exception) Exception not handled by fun() method which is thrown in it. Example (throwing exception) Exception not handled by fun() method which is thrown in it.](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-30.jpg)
![Example (re-throwing exception) Re-throwing exception Example (re-throwing exception) Re-throwing exception](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-31.jpg)
![Example (without exception handling) Consider a scenario where this method is to be implemented Example (without exception handling) Consider a scenario where this method is to be implemented](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-32.jpg)
![Example (with exception handling) Method throws exception object instead of quitting program Invoking method Example (with exception handling) Method throws exception object instead of quitting program Invoking method](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-33.jpg)
![Chained exceptions Chained exceptions](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-34.jpg)
![Chained exceptions • Sometimes, you may need to throw a new exception (with additional Chained exceptions • Sometimes, you may need to throw a new exception (with additional](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-35.jpg)
![Java “throws” keyword • Syntax: type method-name(parameter-list) throws exception-list { // body of method Java “throws” keyword • Syntax: type method-name(parameter-list) throws exception-list { // body of method](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-36.jpg)
![• If method throws checked exception, then either provide try-catch block in that • If method throws checked exception, then either provide try-catch block in that](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-37.jpg)
![Example Example](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-38.jpg)
![Java “finally” keyword • Java finally block is a block that is used to Java “finally” keyword • Java finally block is a block that is used to](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-39.jpg)
![Example Example](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-40.jpg)
![Creating Custom/user-defined Exception Creating Custom/user-defined Exception](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-41.jpg)
![Defining Custom Exceptions • We can create our own Exception sub-classes by inheriting Exception Defining Custom Exceptions • We can create our own Exception sub-classes by inheriting Exception](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-42.jpg)
![Constructors for creating Exception: • Exception( ) • Exception(String msg) • A custom exception Constructors for creating Exception: • Exception( ) • Exception(String msg) • A custom exception](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-43.jpg)
![Example public class Exception. Test { public void show(int i) throws Myexception { if(i>100) Example public class Exception. Test { public void show(int i) throws Myexception { if(i>100)](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-44.jpg)
![Custom Exception (Example 1) Custom/ user-defined exception Custom Exception (Example 1) Custom/ user-defined exception](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-45.jpg)
![Custom Exception (Example 2) Custom/ user-defined exception Custom Exception (Example 2) Custom/ user-defined exception](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-46.jpg)
![Assertion Assertion](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-47.jpg)
![Assertion Assertion](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-48.jpg)
![Example If you use assertion, It will not run simply because assertion is disabled Example If you use assertion, It will not run simply because assertion is disabled](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-49.jpg)
![One important point to understand about assertions is that you must not rely on One important point to understand about assertions is that you must not rely on](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-50.jpg)
![](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-51.jpg)
- Slides: 51
![Exception Exception](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-1.jpg)
Exception
![Exception An exception is an event which occurs during the execution of a Exception • An exception is an event, which occurs during the execution of a](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-2.jpg)
Exception • An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. • An exception is an abnormal condition that arises in a code sequence at run time. • It is an object which is thrown at runtime. • In other words, an exception is a run-time error.
![Runtime errors occur while a program is running if the JVM detects • Runtime errors occur while a program is running if the JVM detects](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-3.jpg)
• Runtime errors occur while a program is running if the JVM detects an operation that is impossible to carry out. Example: Exception occurs at this statement if user enters value 0 for “b” variable
![Exception Handling Exception Handling](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-4.jpg)
Exception Handling
![Exception Handling Exception Handling is a mechanism to handle runtime errors such as Exception Handling • Exception Handling is a mechanism to handle runtime errors such as](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-5.jpg)
Exception Handling • Exception Handling is a mechanism to handle runtime errors such as Class. Not. Found, IO, SQL, Remote etc. Exception at this statement is handled & caught if user enters value 0 for “b” variable
![The JVM firstly checks whether the exception is handled or not If exception is The JVM firstly checks whether the exception is handled or not. If exception is](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-6.jpg)
The JVM firstly checks whether the exception is handled or not. If exception is not handled, JVM provides a default exception handler that performs the following tasks: • Prints out exception description. • Prints the stack trace (Hierarchy of methods where the exception occurred). • Causes the program to terminate. But if exception is handled by the application programmer, normal flow of the application is maintained i. e. rest of the code is executed.
![](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-7.jpg)
![Hierarchy of exception classes Unchecked exceptions Hierarchy of exception classes Un-checked exceptions](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-8.jpg)
Hierarchy of exception classes Un-checked exceptions
![](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-9.jpg)
![Checked Exception Checked Exceptions are those that have to be either caught or Checked Exception • Checked Exceptions are those, that have to be either caught or](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-10.jpg)
Checked Exception • Checked Exceptions are those, that have to be either caught or declared to be thrown in the method in which they are raised. • In other words, those exceptions whose handling is not verified during compile-time, rather they are checked at runtime. • It is a reminder by compiler to programmer to handle failure scenarios. • For example, the below I/O statement has to be written in try block: File. Input. Stream fp = new File. Input. Stream("C: /a. txt"); • When to use: Un-handled exception. Compiler forces to write it inside try block. • Operation where chances of failure are more eg: IO operation, database access, networking operation, etc.
![Unchecked Exception Unchecked exceptions are those exceptions whose handling is not verified during Unchecked Exception • Unchecked exceptions are those exceptions whose handling is not verified during](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-11.jpg)
Unchecked Exception • Unchecked exceptions are those exceptions whose handling is not verified during compile-time, rather they are checked at runtime. • It is direct sub-class of Runtime. Exception. • For example, the below statement need not to be written in try block: int val = a/0; • Advantage: maintains code readability. Compiler doesn’t force to write it inside try block. • They arise due to: • Programming errors (like accessing method of null object, accessing element outside array) s. Exception
![Commonly used subclasses of Exception Arithmetic Exception Array Index Out Of Bounds Commonly used sub-classes of Exception • Arithmetic. Exception • Array. Index. Out. Of. Bounds.](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-12.jpg)
Commonly used sub-classes of Exception • Arithmetic. Exception • Array. Index. Out. Of. Bounds. Exception • Number. Format. Exception • Null. Pointer. Exception • IOException
![](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-13.jpg)
![](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-14.jpg)
![Types of exceptions Types of exceptions](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-15.jpg)
Types of exceptions
![Hierarchy of exception classes Unchecked exceptions Hierarchy of exception classes Un-checked exceptions](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-16.jpg)
Hierarchy of exception classes Un-checked exceptions
![Classification The exception classes can be classified into three major types 1 system errors Classification The exception classes can be classified into three major types: 1) system errors](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-17.jpg)
Classification The exception classes can be classified into three major types: 1) system errors caused by internal system errors 2) exceptions caused by your program and by external circumstances 3) runtime exceptions caused by programming errors Exceptions System errors Runtime exceptions
![System errors System errors are thrown by the JVM and are represented in System errors • System errors are thrown by the JVM and are represented in](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-18.jpg)
System errors • System errors are thrown by the JVM and are represented in the Error class. • The Error class describes internal system errors, though such errors rarely occur. If one does, there is little you can do beyond notifying the user and trying to terminate the program gracefully.
![Exceptions Exceptions are represented in the Exception class which describes errors caused by Exceptions • Exceptions are represented in the Exception class, which describes errors caused by](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-19.jpg)
Exceptions • Exceptions are represented in the Exception class, which describes errors caused by your program and by external circumstances. These errors can be caught and handled by your program.
![Runtime exceptions Runtime exceptions are represented in the Runtime Exception class which describes Runtime exceptions • Runtime exceptions are represented in the Runtime. Exception class, which describes](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-20.jpg)
Runtime exceptions • Runtime exceptions are represented in the Runtime. Exception class, which describes programming errors. • Example: bad casting, accessing an out-of-bounds array, and numeric errors. • Runtime exceptions are generally thrown by the JVM.
![](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-21.jpg)
![or with both or with both.](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-22.jpg)
or with both.
![It contains statements that are to be executed when the exception represented by the It contains statements that are to be executed when the exception represented by the](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-23.jpg)
It contains statements that are to be executed when the exception represented by the catch block is generated. If program executes normally, then the statements of catch block will not executed. If no catch block is found in program, exception is caught by JVM and program is terminated.
![](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-24.jpg)
![Defining Generalized Exception Handler A generalized exception handler is one that can handle Defining Generalized Exception Handler • A generalized exception handler is one that can handle](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-25.jpg)
Defining Generalized Exception Handler • A generalized exception handler is one that can handle the exceptions of all types. • If a class has a generalized as well as specific exception handler, then the generalized exception handler must be the last one. class Divide{ public static void main(String arr[]){ try { int a= Integer. parse. Int(arr[0]); int b= Integer. parse. Int(arr[1]); int c = a/b; System. out. println(“Result is: ”+c); } catch (Throwable e) { System. out. println(e); } } }
![Key points At a time only one Exception is occurs and at a Key points • At a time only one Exception is occurs and at a](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-26.jpg)
Key points • At a time only one Exception is occurs and at a time only one catch block is executed. • All catch blocks must be ordered from most specific to most general i. e. catch for Arithmetic. Exception and Array. Index. Out. Of. Bounds. Exception must come before catch for Exception. The program will not compile since all the exceptions are caught in 1 st catch block. Hence, “code un-reachable” error.
![Nested Trys class Nested Try Demo public static void mainString args try Nested Try’s class Nested. Try. Demo { public static void main(String args[]){ try {](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-27.jpg)
Nested Try’s class Nested. Try. Demo { public static void main(String args[]){ try { int a = Integer. parse. Int(args[0]); try { int b = Integer. parse. Int(args[1]); System. out. println(a/b); } catch (Arithmetic. Exception e){ System. out. println(“Div by zero error!"); } } catch (Array. Index. Out. Of. Bounds. Exception) { System. out. println(“Need 2 parameters!"); } } }
![Java throw keyword It is used for explicit exception throwing throw Exceptionobject Java “throw” keyword • It is used for explicit exception throwing. throw <Exception-object>; •](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-28.jpg)
Java “throw” keyword • It is used for explicit exception throwing. throw <Exception-object>; • ‘throw’ keyword can be used: • to throw user defined exception • to customize the message to be displayed by predefined exceptions • to re-throw a caught exception Note: System-generated exceptions are automatically thrown by the Java run-time system.
![Example throwing exception Example (throwing exception)](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-29.jpg)
Example (throwing exception)
![Example throwing exception Exception not handled by fun method which is thrown in it Example (throwing exception) Exception not handled by fun() method which is thrown in it.](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-30.jpg)
Example (throwing exception) Exception not handled by fun() method which is thrown in it. So, it has to be handled in the calling method (i. e method which invoked fun() method)
![Example rethrowing exception Rethrowing exception Example (re-throwing exception) Re-throwing exception](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-31.jpg)
Example (re-throwing exception) Re-throwing exception
![Example without exception handling Consider a scenario where this method is to be implemented Example (without exception handling) Consider a scenario where this method is to be implemented](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-32.jpg)
Example (without exception handling) Consider a scenario where this method is to be implemented by any other java developer Method quitting program to avoid runtime exception Invoking method to perform calculation
![Example with exception handling Method throws exception object instead of quitting program Invoking method Example (with exception handling) Method throws exception object instead of quitting program Invoking method](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-33.jpg)
Example (with exception handling) Method throws exception object instead of quitting program Invoking method to perform calculation
![Chained exceptions Chained exceptions](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-34.jpg)
Chained exceptions
![Chained exceptions Sometimes you may need to throw a new exception with additional Chained exceptions • Sometimes, you may need to throw a new exception (with additional](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-35.jpg)
Chained exceptions • Sometimes, you may need to throw a new exception (with additional information) along with the original exception. This is called chained exceptions.
![Java throws keyword Syntax type methodnameparameterlist throws exceptionlist body of method Java “throws” keyword • Syntax: type method-name(parameter-list) throws exception-list { // body of method](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-36.jpg)
Java “throws” keyword • Syntax: type method-name(parameter-list) throws exception-list { // body of method } • This is necessary for all exceptions, except those of type Error or Runtime Exception, or any of their subclasses. • All other exceptions that a method can throw must be declared in the throws clause. If they are not, a compile-time error will result.
![If method throws checked exception then either provide trycatch block in that • If method throws checked exception, then either provide try-catch block in that](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-37.jpg)
• If method throws checked exception, then either provide try-catch block in that method or write “throws” keyword at the method header. • If “throws” keyword is used at the method header, then the calling method must handle the exception which could be thrown from the called method.
![Example Example](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-38.jpg)
Example
![Java finally keyword Java finally block is a block that is used to Java “finally” keyword • Java finally block is a block that is used to](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-39.jpg)
Java “finally” keyword • Java finally block is a block that is used to execute important code such as closing connection, stream etc. • “finally” creates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block. • The “finally” block will execute whether or not an exception is thrown. • If an exception is thrown, the “finally” block will execute even if no catch statement matches the exception. • If a “finally” block is associated with a try, the “finally” block will be executed upon conclusion of the try. • The “finally” clause is optional. However, each try statement requires at least one catch or a “finally” clause. • Note: 1) For each try block there can be zero or more catch blocks, but only one finally block. 2) The finally block will not be executed if program exits(either by calling System. exit() or by causing a fatal error that causes the process to abort).
![Example Example](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-40.jpg)
Example
![Creating Customuserdefined Exception Creating Custom/user-defined Exception](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-41.jpg)
Creating Custom/user-defined Exception
![Defining Custom Exceptions We can create our own Exception subclasses by inheriting Exception Defining Custom Exceptions • We can create our own Exception sub-classes by inheriting Exception](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-42.jpg)
Defining Custom Exceptions • We can create our own Exception sub-classes by inheriting Exception class. • The Exception class does not define any methods of its own. • It inherits those methods provided by Throwable. • Thus, all exceptions, including those that we create, have the methods defined by Throwable available to them.
![Constructors for creating Exception Exception ExceptionString msg A custom exception Constructors for creating Exception: • Exception( ) • Exception(String msg) • A custom exception](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-43.jpg)
Constructors for creating Exception: • Exception( ) • Exception(String msg) • A custom exception class is represented by a subclass of Exception / Throwable. • It contains the above mentioned constructor to initialize custom exception object.
![Example public class Exception Test public void showint i throws Myexception ifi100 Example public class Exception. Test { public void show(int i) throws Myexception { if(i>100)](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-44.jpg)
Example public class Exception. Test { public void show(int i) throws Myexception { if(i>100) throw new Myexception(i); Here object of custom class is created and thrown else System. out. println(+i+" is less then 100 it is ok"); class Myexception extends Throwable } public static void main(String []args){ { int i=Integer. parse. Int(args[0]); public Myexception(int i) int j=Integer. parse. Int(args[1]); { Exception. Test t=new Exception. Test(); System. out. println("you have entered. " +i +" : It exceeding the limit"); try{ t. show(i); } } catch(Throwable e) { System. out. println("catched exception is "+e); } } t. show(j);
![Custom Exception Example 1 Custom userdefined exception Custom Exception (Example 1) Custom/ user-defined exception](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-45.jpg)
Custom Exception (Example 1) Custom/ user-defined exception
![Custom Exception Example 2 Custom userdefined exception Custom Exception (Example 2) Custom/ user-defined exception](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-46.jpg)
Custom Exception (Example 2) Custom/ user-defined exception
![Assertion Assertion](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-47.jpg)
Assertion
![Assertion Assertion](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-48.jpg)
Assertion
![Example If you use assertion It will not run simply because assertion is disabled Example If you use assertion, It will not run simply because assertion is disabled](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-49.jpg)
Example If you use assertion, It will not run simply because assertion is disabled by default. To enable the assertion, -ea or -enableassertions switch of java must be used.
![One important point to understand about assertions is that you must not rely on One important point to understand about assertions is that you must not rely on](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-50.jpg)
One important point to understand about assertions is that you must not rely on them to perform any action actually required by the program. The reason is that normally, released code will be run with assertions disabled. This statement will not execute if assertion is not enabled using “-ea” switch
![](https://slidetodoc.com/presentation_image_h2/891922a049dd22b1553a3f41d90abeaa/image-51.jpg)
Near miss sentinel event
Simple and compound events
Independent event vs dependent event
Independent event vs dependent event
5ws in event management
Bridge breaks in central java the text tells us about
Moscow a russian journalist has uncovered evidence
Stagecoach narrative structure
Napoleonic code women
Which event from acts ii and iii
Extends and implements difference
System argument exception
Robust programs
Interface foo int k=0
Class a void foo() throws exception
Throw exception php
Unt concur
Phenotypic characteristics of bacteria
Runtimeexception extends exception
What is exception
Exception vs error in java
Exception handling pl sql
Exceptions of mendel's law
Java exception hierarchy
Vb net error handling
Exception handling in java
Charting by exception pros and cons
Dasar exception
Exception of koch postulates
Apa itu exception
Goodsalls rule
Mips
Usfws designated ports
No data found exception in oracle
Exception handling pada java
Vb.net nvl
Contoh model pie
Java exception error
Fiq irq
Http data source exception
Event handling in ada
Liraglutide ramq
Php global exception handler
What is exception in computer architecture
A programmer can choose to ignore an unchecked exception.
Apa yang dimaksud dengan exception
Exception
Superlative the old
Software interrupts
Tceq wud
Private pilot limitations and privileges
What is a friend class