Exception Handling
Exception Handling
Exception:
If there is an exception in our program, the program will be terminated. But we can save program by using exception handling techniques.
Types of Exception:
- Checked Exception: The exception which are detected at compilation time by JVM are known as Checked exception.
- Unchecked Exception: The exception which are detected at run time by JVM are known as Unchecked Exception.
Error:
If there is an error in your program, the program will be terminated. We can't save our program.
Types of Error:
- Compile time Error: These errors occurs due to wrong syntax.
- Runtime Error: These errors occurs due to inefficiency of the machine.
- Logical Error: These errors are occurs due to bad login in program.
Key points In Exception Handling:
- If we are writing try-catch-finally blocks then we should maintain the order.
- try-catch-finally ---> Valid
- catch-try-finally ----> Invalid
- finally-catch-try ----> Invalid
- There should not be any statement between try-catch-finally.
- Always it is suggestable to write minimum code inside the try block.
- If an exception occured inside the try block immediately the compiler will be going to the catch block. Rest of the code which is present after the exception inside the try will not be executed.
- A try block never exists without catch or finally[upto Java 1.4v].
- try-catch ----> Valid
- try-finally ---> Valid
- We can use a single try block that is try with resources from Java 1.7v onwards.
- try(resource){}
- For a single try block we can write multiple catch block but there should be only one finally block.
- If we are writing multiple catch blocks always we need to catch first child exception.(we need to follow exception hierarchy from botton to top).
- Fron Java 1.7v onwards we can handle multiple exceptions by a single catch block also.()
Comments
Post a Comment