Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

adplus-dvertising
Exception Handling
Previous Home Next

Exception is error that raised in programs during run time. Exception can be raise bcause:

  1. A user has entered invalid data or

  2. A file that needs to be opened cannot be found or

  3. A network connection has been lost in the middle of communications, or the JVM has run out of memory.

Except above there may be many other reasons.

When Exception raise in method it creates an object. This object called Exception Object contains all information regarding error and state of program.

Exception Handling Mechanism

When Exception object raised it must be handle otherwise program will terminate. Java use five key words called exception handling

  1. try
  2. catch
  3. throw
  4. throws
  5. finally
The general form of exception handling block is:

 try
{
//block monitors error
}
catch(exceptiontype e)
{
//exception handler
}
finally
{
// block will must execute before try ends
}

Try block contains code where exception may generate. Exception raised caught by catch block. The code that must execute before a method returns or program terminated must be written in finally block. Exception type shows type of exception raised . One example of Java exception is given below.

Example


/*
 * Saved as excpexmp1.java
 * Show use of try and catch block
 */
package exceptionhandling;
 class Excp{
    int dividend,divisor;
    Excp(int n1,int n2)
    {
        divisor=n1;
        dividend =n2;
    }
    public int div()
    {
        int i=0;
        try{
         i=dividend/divisor;
        }catch(ArithmeticException e)
        {
        System.out.println("Divide by zero");
        }
        return i;
    }
}
public class excpexmp1 {
    public static void main(String[] str)
    {
     
     Excp obj=new Excp(10,20);
     Excp ob1= new Excp(0,10);
     System.out.println(obj.div());
     System.out.println(ob1.div());
    }
}

Output:


2
Divide by zero
0

Previous Home Next