try with multiple catch blocks / Chapter 6 / Exception Handling

try with multiple catch blocks


A way of handling an exception is varied from exception to exception hence, for every exception type, it is highly recommended to take separate catch block.

Let's take two examples of try catch.

Bad Or Worst Programming Practice
try 
{
Risky Code
}
catch (Exception e)
{
Exception Handling code
}

Best and Recommended Programming Practice

Method To Print Exception Information /Chapter 5 / Exception Handling

Method To Print Exception Information:-


The Throwable class defines the following methods to print Exception Information.Methods to print exception information

java try catch / Chapter 4 / Exception Handling

java try catch


java try catch block is highly recommended to handle exceptions.

The code which may rise an exception is called risky code and we have to define that code inside try block and corresponding handling code we have to define inside catch block.
try 
{
Risky Code
}
catch (Exception e)
{
Exception Handling code
}

let's take an example of a java program without try catch

Checked and Unchecked Exceptions /Chapter 3/ Exception Handling

Checked Exceptions


Checked Exceptions


The exceptions which are checked by the compiler for smooth execution of the program at runtime are called, checked exceptions.

example:-

FileNotFoundException,ServletException etc.

In our java program, if there is a possibility of rising checked exception then mandatory we should handle that checked exception (either by try catch or throws keyword) otherwise, we will get compile time error.

Java Exception Hierarchy / Chapter 2/ Exception Handling

java exception hierarchyJava Exception Hierarchy



  • The Throwable class is the root class for java exception hierarchy.

  • The Throwable class defines two child classes.



  1. Exception

  2. Error


1.Exception:-


Most of the time exceptions are occurs by our java program and these are recoverable.

for example: -


In our java program, there is a requirement of reading data from a remote file which is locating at NewYork (USA). At runtime, if NewYork file is not available our program should not be terminated abnormally and we will get RunTime exception saying"FileNotFoundException". To solve this problem we have to provide some local file to continue rest of the program normally.

Introduction of Exception Handling /Chapter -1 / Exception Handling

java exception

Java Exception:-


An unexpected unwanted event that disturbs normal flow of the program, is called an exception.

The purpose of exception handling:-


The main objective of exception handling is "graceful termination " of the program. It is highly recommended to handle the exception in java program.

Explanation:-


Exception Handling doesn't mean repairing an exception. We have to provide an alternative way so that the rest of the program will execute normally, is the concept of exception handling.


for example: -

Difference between interface and abstract class / Chapter 14 / OOPs Concept

Difference between interface and abstract class

Difference between interface and abstract class



  1. In the interface, if we don't know about implementation and just we have requirement specification then we should go for an interfaceBut In the abstract class, If we are talking about partial implementation but not complete then we should go for abstract class.

  2. In the interface, every method is always public and abstract whether we are declaring or not. But  In the abstract class, every method need not be public and abstract. we can take concrete method also.

Java Interface /Chapter -13 /OOPs Concept


Java Interface



  • Any service requirement specification (SRS) is considered as an "interface".

  • Inside "interface", every method is always abstract whether we are declaring or not hence, the interface is considered as 100% pure abstract class.

  • Any contract between client and service provider is considered as an "interface".


Interface declaration and implementation



  1. For each and every method present in the interface we have to provide implementation otherwise, we have to declare the class as abstract then next level child class is responsible for providing the implementation.

  2. Every method present in an interface is always public and abstract we are declaring or not hence, whenever we are implementing an interface's for every method compulsory we should declare as public otherwise we will get Compile-Time Error.


Constructor in Java /Chapter -12 /OOPs Concept

java constructorjava constructor


Whenever we are creating an object some part of the java program(code) will be executed automatically to perform initialization of the object, this part of the program is called "Constructor".
Hence the main objective of the constructor is to perform initialization of an object. 

example 1.

class Employee

{

String name;

int eid;

Employee (String name, int eid)

{

this.name = name;

this.eid = eid;

System.out.println(name+"-----"+eid);

}

public static void main(String args[ ])

{

Employee e1 = new Employee("Dhoni",101);

Employee e2 = new Employee("Virat",102);

}

}

Output of the above program is:-

In how many ways we can create Object in JAVA?/Chapter-11/OOP's Concept

In how many ways we can create Object in JAVA?In five ways we can create Object in java.




  1. By using the new operator:-In how many ways we can create Object in JAVA?




  2. By using newInstance( ) method:- In how many ways we can create Object in JAVA?




  3. By using the clone( ) method:- 

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com