Java | Exception | Try Cath & Finally :
~~ CORE-JAVA ~~
1. INTORDUCTION
1.1 Java Intorduction
1.2 Java history
1.3 Java features
1.4 Diff b/w Java & C++
1.5 Java Variables
1.6 Java Data Types
1.7 Typecasting
1.8 Arrays
2. OOPS
2.1 Introduction
2.2 Class
2.3 Object
2.4 Static Keywords
2.5 Constructors
2.6 This KeyWord
3. INHERITANCE
3.1 Inheritance (IS-A)
3.2 Aggregation (HAS-A)
4. POLYMORPHISM
4.1 Polymorphism & Its Type
4.2 Method Overloading
4.3 Method Overriding
4.4 Super Keyword
4.5 Final Keyword
4.6 Dynamic Binding
5. ABSTRACTION
5.1 Abstract Classes & Methods
5.2 Interfaces
5.3 Abstract vs Interface
6. ENCAPSULATION
7. STRING MANIPULATIONS
7.1 String
7.2 String Buffer
7.3 String Tokenizer
8. PACKAGES
8.1 Predefined packages
8.2 Userdefined Packages
8.3 Access Specifiers
9. EXCEPTION HANDLING
9.1 Introduction
9.2 Try-Catch-Finally
9.3 Throws-Throw Keyword
9.4 Built-in Exception
9.5 Customized Exception
10. MULTITHREADING
10.1 Introduction
10.2 Thread Creations
10.3 Thread Life Cycle
10.4 Life Cycle Methods
10.5 Synchronization
10.6 Wait() notify() notify all()
11. WRAPPERCLASSES
11.1 Introduction
11.2 Wrapper Classes
12. COLLECTION FRAME WORK
12.1 Introduction
12.2 List interface
12.3 Set interface 12.4 Map interface
13. INNER CLASSES
13.1 Introduction
13.2 Member inner class
13.3 Static inner class
13.4 Local inner class
13.5 Anonymous inner class
14. CoreJava Interview Questions
Exception Handling :
- Whenever Exceptions occur in our program, it is highly recommended to handle it.
- Exception Handling doesn’t means repairing or handling an Exception. It means defining an alternative way so that rest of the program can continue.
- When there will be some exception, it should be handle by the Programmer very carefully, for this we need to follow below steps :
- Step 1 : Programmer should observe the statement very carefully and where may be a chance of exception, we need to put these statement under try block as below :
try {
statements;
}
- Step 2 : The Programmer should write the Catch block where he should display the exceptions detail to the user. So that he can understand what’s happen wrong. Catch block declaration looks like as below :
Catch(Exceptionclass ref)
{
statements;
}
- Step 3 : Lastly Programmer should perform Clean up operations like closing the files or closing the connections or terminating the threads, etc. So for that purpose we need to use Finally block. This block will execute always in case of exceptions or in case of without exception. Below is an syntax for it :
finally {
statements;
}
- Step 1 : Programmer should observe the statement very carefully and where may be a chance of exception, we need to put these statement under try block as below :
Example :
sasdsdadadda
Methods to Print Exception Information :
- Throwable Class defines the following below methods to Print Exception Information :
- PrintStackTrace()
- This method prints Name of the exception and description both.
- toString()
- This method also prints exception and description both.
- getMessage()
- This method prints only description of the exception.
- PrintStackTrace()
Example :
Try with Multiple Catch Block :
- Most of the times there is possibility of more than one exception can occur in our program, In this case Programmer should write multiple catch block for each of them as below :
Example :
- But sometimes, there is possibilities we don’t know which type of exception is going to throw our program. So in that case we can use as below, but it’s not recommended way. The above one is only recommended way to handle :
Catch (Exception e) {
//default exception handler;
}
Example :
Difference between final vs finally vs finalize :
- Final :
- It is a modifier, which is applicable for Class, methods and variables.
- If Class is declared as final, then child class creation is not possible here.
- If Method is declared as final, then overriding of that method is not possible.
- If Variable is declared as final, then we can’t change it’s value.
- It is a modifier, which is applicable for Class, methods and variables.
- Finally :
- It is block, always use in exception handling with try-catch to maintain clean up code.
- In exception handling finally block will execute always whether exception raised or not.
- Finalize() :
- It is a method, which is executed by Garbage Collector.
- Garbage Collector execute this method before destroy any object for clean up activity.