Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Thursday, 2 January 2020

Java Exception Handling


Error is a serious problem caused during the runtime execution of a program. It cannot be handled and recovered. Errors cause the program to terminate. For example, System Crash or Out of Memory.

Exception is an error which can disrupt the normal execution flow of a program and may cause termination of the program.

During the normal execution of a program, whenever an error is occurred, an exception object is created at runtime, the execution of the normal flow is stopped, and JRE(Java Runtime Environment) finds a suitable handler for that exception. The exception object contains information like Type of Exception, Line number of statement along with Method call hierarchy, etc. The process of creating an exception object and passing it to the suitable handler is called throwing the exception.

Since the exception is an object in Java, it has a Parent Class as well i.e. Throwable. Throwable further has 2 child classes i.e. Error, and Exception.
Error is a scenario which cannot be handled and results in the prompt termination of the program. Whereas, an Exception is a scenario which only halts the normal execution flow and terminates the program only if the scenario is not handled.

Exception further be classified into Checked and Runtime Exception.
Checked Exception - These are the type of exception which are anticipated during the time of writing the code. Adding informative log messages can help in debugging this type of exception. These are also called Compile Time Exception. Before compilation, compiler verifies whether the exception has been handled or not. If not, then compilation error is thrown.
Runtime Exception - This type of exception cannot be anticipated. Checking the data before processing can avoid this type of exception. These are also called Unchecked Exception.


Keywords:

  • throws
    • When we are explicitly throwing an exception in a method and not handling it, we use "throws" keyword to let the calling method know that there might be an exception which needs to be handled. In this case, the caller method can also use throws keyword and not handle the exception and let its caller method know about the exception.
  • throw
    • When we want to explicitly throw an exception, "throw" keyword is used.
  • try - catch
    • try- catch is basically a block of code used to handle exception. The statements which are prone to exception are placed inside a try block and a handler for those exception(s) is written inside the catch block. The catch block expects an parameter, i.e. the Type of Exception. We can have a try-catch block inside another try-catch block and we can also have multiple catch blocks for a single try block to catch different exceptions.
  • finally
    • There is an optional block of code used with try-catch block in Java Exception Handling called the "finally" block. Since exceptions halt the normal flow of execution, there might be some code which needs to be executed e.g. closing the files or database connections, if opened, etc. Those blocks of code are written in finally block. This block of code gets executed irrespective of the occurrence of the exception.

Points to remember:
  • Catch and Finally blocks cannot be used without using Try block.
  • Try block must have a supporting Catch and/or Finally block.
  • Only one Finally block can be used with a Try-Catch block.
  • When using multiple catch blocks with a single try block, the exceptions must be ordered on the basis of most specific to general.
  • Finally block is not executed only if the program terminates abruptly(due to death of thread) or the program is explicitly terminated or there has been an exception in finally block as well.
  • In case of nested try-catch blocks, if the catch block does not handle the exception, then the catch block of the parent try block is inspected for that exception, and if a match is found then that catch block is executed. But in case the parent try block also does not handle the exception, then a system generated exception is shown same as what we see when we do not handle the exception.
  • Statements present in finally block execute even if control transfer statements like return, continue, and break are used in the try block.
  • Error and RuntimeException are collectively known as Unchecked Exception.

In addition to using the Java built-in classes, we can create your own exception(both Checked Exception and Runtime Exception) according to the needs of your project. We can create a user defined checked exception by extending Exception class and we can extend RuntimeException class to create a user defined Runtime Exception.

Wednesday, 1 January 2020

Java Interview Questions

  1. Run time and Compile time Polymorphism
  2. Overload Main Method
  3. Program Without Main Method
  4. Override Static Method
  5. Object Oriented Programming over Procedural Programming
  6. Abstract Classes VS Interfaces
  7. Clubbing Exceptions in Catch Block
  8. Try with Resources, Suppressed Exception, and Retrieving Suppressed Exception
  9. Possibility when Finally Block is Not Executed
  10. Exception Handling with Method Overriding
  11. Explain Struts framework
  12. Difference between request.getParameter() and request.getAttribute()
  13. Difference between REST and SOAP web service
  14. Difference between jsp:include and jsp:forward
  15. How to create a Servlet
  16. Brief about Collections hierarchy
  17. Difference between different collection classes
  18. Working of HashMap
  19. Difference between Git Rebase and Git Reset
  20. Brief about @Qualifier annotation in Spring
  21. Explain different REST methods
  22. Difference between Spring and SpringBoot
  23. Explain Spring framework workflow
  24. Explain SpringBoot framework workflow
  25. Brief about contract between equals() and hashCode()
  26. Useful methods in Java classes like Character, Arrays, String, Collections, Object
  27. Difference between Comparator and Comparable
  28. Difference between sleep, yield and wait methods
  29. Can we create volatile array
  30. Explain Cyclic Barrier in Multithreading
  31. Difference between Association, Composition and Aggregation
  32. What are different Application Context classes in Spring
  33. Which classes are available in Spring for Handshake
  34. Difference between Procedure and Function in PLSQL
  35. Can we implement an interface without using implements
  36. Brief about JSP and Servlet lifecycle
  37. Explain CSRF, XSS, Brute Force, Denial of Service and CORS
  38. Deep Copy vs Shallow Copy
  39. Brief about Exception handling and Runtime, and Compile time exception (Read here)
  40. Difference between webserver and appserver.

Sunday, 25 March 2018

OOPs Concepts

Although its actually OOP concepts, but the most widely used term is OOPs Concepts. Though OOP stands for Object Oriented Programming, but what what does "s" stands for? System. "s" in OOPs stands for Object Oriented Programming System.

Lets talk about OOP concepts, the basics of all Programming Languages available today.
  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritence

Abstraction
Abstraction is the concept of hiding the internal processing from outside world. We can implement abstraction using encapsulation and inheritance.

Encapsulation
Encapsulation is a means to achieve Abstraction. it is used to restrict the scope and usage of class members and methods. Access modifiers such as public, private and protected are used to restrict access.

Polymorphism
Polymorphism is a concept in which an object can behave differently based on different situations.
Polymorphism can further be classified into 2 categories, i.e. Run time Polymorphism, and Compile time polymorphism.

Inheritance
Inheritance is a means to achieve code reusability. It allows the properties of another class to be used in another class in a Parent-Child model. The class using the properties of another class is called the Derived class and the class whose properties are used by another class are called Base Class or Parent Class. Inheritance in Java is achieved using keyword "extends".
Types of Inheritance(in Java): Single Inheritance, Multilevel Inheritance, and Hybrid Inheritance