Friday 7 July 2017

try() - catch() Block

There are 5 keyword used in Exception such as:-
1)    try
2)   catch
3)   finally
4)   throw
5)   throws

1)  try Block: 

Its used to place code that may throw exception. It must be used with catch or finally block.
syntax are given below.

A)   syntax:-  try and catch block:-

         try{
                   // code that may be exceptions occurs
             }
         catch(Exception_class name ref )
            {
                  //code
            }

B)  syntax:- try and finally

          try{
                 //code where may be exception occurs
             }
          finally(){}


Example:write a simple program without exception handling.

     public class A{   
       public static void main(String args[]){   
        int a=10/0;//may throw exception   
             System.out.println("some other code...");   
       }  }

output:- 

 Exception in thread "main" java.lang.ArithmeticException: / by zero
          at A.main(A.java:8)




 In example 1 occur exception and "some other code" statement of  the program are not printed here.
so lets write a program to solve the problem.



Example:- program to handle exception .

public class ExceptionExample {
public static void main(String[] args) {
    try{
        int a=10/0;
        }
    catch(Exception e)
    {
        System.out.println(e);}

System.out.println("some other code");
}}
output: 
java.lang.ArithmeticException: / by zero
some other code



Now in this program we used to try{}block and catch(){}block so printed the "some other code' statement in the program.



2)  catch block:- 

 It is used to handle exception. It must be used with try block.

  Syntax:-   

      try
           {    
                  //code where may be exception occure
            }
      catch(Exception_class_ref)
           {
                  //code
           }


Example:- 

import java.util.concurrent.ExecutionException;
public class ExceptionCatchBlock {
public static void main(String[] args) {
    try{
        String s1=null;
        System.out.println(s1.length());
        }

   catch(Exception e){System.out.println(e);}
    System.out.println("other code");
 }
 }

output:- 

java.lang.NullPointerException
other code


Example:- Write a program to use catch block with array. 

public class a2 {
public static void main(String[] args) {
    try{
        int[] a=new int[5];
         a[5]=20;
      System.out.println(a[5]);}
     catch(Exception  e){System.out.println(e);
           System.out.println("some other code");
    }}}
 

output:- 

java.lang.ArrayIndexOutOfBoundsException: 5
some other code






I will describe finally throw and throws  further.

Tuesday 27 June 2017

What is Exception handling?

Exception Handling


Questions:-

What is exception handling?

Answer:-

In java,exception is an object or event which is thrown at run-time and it is mechanism to handled run time-error.

what is different between in Error and Exception:-

there are some type of error such as:-

1) Syntax error.

2) Logical error.

3) Run-time error.

Syntax Error-

Syntax error is an error in the syntax a sequence of character.it is cause by a programmer because writing mistake of the programmed. for example:-

1) int a=10;   // it is correct.

2) int 20=b;    // it is not correct so that is a syntax error.

Logical Error-

Logical error is an error, which is extremely find to difficult in the program because they reflect the coding.we can say that logical error is bug which is in the source code such as operator precedence and any number divide by zero.
Let's see Example-

1)   int a=10.5;

2)   for(i=1;i<=10;i++);

Run-Time Error-

Run-time error is also indicate the bugs in the program at the run-time. For Example-

An integer divided by Zero.
int a=20/0;  //  here occurs run-time error.

In run-time error has two part or we can divided in two parts.

1) Handled Error.(It is also called an Exception Error.)

2) UN-handled Error.

Advantages-

Rest of the code is executed or normal flow of the program and an application is maintained.
For Example-

public class ExceptionExample{
public static void main(String[] args){
try{
int a=20/0;
}
catch(Exception e){
System.out.println("rest of the code");
}}

There are two type of Exception:-
1)  Checked.
2)  Unchecked.
Java Exception Hierarchy-



Friday 19 May 2017

Java Expert Writer













Hello Friends welcome in java expert writer,
Today I write some content  about Java Programming Language,

What is java programming language?
Java is a programming language and platform, it was developed by Sun Microsystem in 1995. Java is a Object Oriented programming language and there are some feature such as:-
  1. Simple 
  2. Object-Oriented
  3. Secure
  4. Portable
  5. Platform Independent
  6. Robust
  7. Architecture Neutral
  8. Dynamic
  9. Interpreted
  10. High Perfomance
  11. Multithreaded
  12. Distributed
let's see simple java program:-

class A{
public static void main(String args[ ])
{
System.out.println("hello java");
}}   


·      Simple:- 
If  we know about c++ programming language ,so that we can learn easily because it is use the syntax  of  c++ programming language , so that java a simple language and there is no use of explicit pointer, operator overloading.

Object-Oriented:-
Object-oriented programming (OOP) is a programming paradigm based on the concept of “object”, which has state and behavior.
Basic concept of OOPs:- 
  1. Object
  2. Class
  3. Inheritance
  4. Polymorphism
  5. Abstraction
  6. Encapsulation

Object And Class 


Class:- Class is blueprint of an object, we can also say that class  is define state and behavior of object. State is called data and behavior is called method. class includes variables,methods,constructors etc.

Object:-  An Object is an entity and object of the class will have method and variables. In the other word  object is a physical as well as  logical entity.For example bike,table,chair,fan,television etc.


Figure:-  object class diagram

Now I will discus other oops concept later.

Portable and Independent:-


figure:- Portable and Independent


Why java independent:-

Java is a platform independent, Because when we  install  jdk software on our system then automatically JVM are installed on our system. For every operating system separate JVM is available which is capable to read the .class file (byte code). So we will be able to run it on Windows, Linux, OS X etc.