Showing posts with label exception. Show all posts
Showing posts with label exception. Show all posts

Sunday, 7 December 2014

How to create custom Exception in Java

How to create custom Exception in Java

Custom Exception

Why we need custom Exception ?
How to create a custom exception ?
Write code to create a Custom Exception

All these are questions usually asked in a Java interview .So my today's post on how to create a custom exception in java .

As we know There are two types of Exception :
1. Checked Exception
2. Unchecked Exception

Both these exceptions have a common parent class Exception .So ,in same order if we have to create a custom exception ,we need to extend Exception class.

Ok , let me explain the process step by step .

1. Any custom exception should extend Exception Class .

2. There should be a proper name given to the Exception ,For example if there is an exception related to Invalid age name can be given as InvalidAgeException
public class InvalidAgeException extends Exception {}



public class InvalidAgeException extends Exception {
private int age;

public InvalidAgeException ()
{
super();
}
public InvalidAgeException (String message, int age)

{
super(message);

this.age = age;

} 

}


In the above way we can define two constructors of InvalidAgeException with message and without message

Usage of custom Exception:

Below is the program with usage of custom exception.

public class CustomExceptionDemo 


{ 

private static final Map<Integer, String> employee = new HashMap<>(); 

static

 {
employee.put(100, "Mahesh");

employee.put(101, "Suresh");

 employee.put(102, "Bran");

 employee.put(103, "Troy"); }

 public static void main(String args[]) { 

CustomExceptionDemo t = new CustomExceptionDemo();

t.getAge(1000); } 

public String getAge(int age)

 { 

if (employee.get(age) == null)

{ 

throw new InvalidAgeException ("No such employee exists", age);

} return employee.get(name);

}

}





SO friends this was my post on Custom Exception .
Read More »

Saturday, 13 September 2014

Exception ,Types of Exception , Its Handling


Exception Handling

What is Exception

Exception is an abnormal condition. In java, exception is an event that disrupts the normal flow of the program. It is an object which is thrown at run time.

Exception Handling

The core advantage of exception handling is to maintain the normal flow of the application. Exception normally disrupts the normal flow of the application that is why we use exception handling.

Types of Exception

1.Checked Exception
2. Unchecked Exception
3. Error

How to reverse a string in java


1. Checked Exception

A checked exception is an exception that is typically a user error or a problem that cannot be foreseen by the programmer.

2. Unchecked Exception/ RunTime Exception

A runtime exception is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of compilation.


How to Catch an Exception


A method catches an exception using a combination of the 
try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following:

try
{
   //Protected code
}catch(ExceptionName e1)
{
   //Catch block
}

-------------------------------------------------------------------------------------------------------------------------
Exception Handling Example

public class MyExceptionHandle {
    public static void main(String a[]){
        try{
            for(int i=5;i>=0;i--){
                System.out.println(10/i);
            }
        } catch(Exception ex){
            System.out.println("Exception Message: "+ex.getMessage());
            ex.printStackTrace();
        }
        System.out.println("After for loop...");
    }
}



Output:

2
2
3
5
10
Exception Message: / by zero
java.lang.ArithmeticException: / by zero
        at com.myjava.exceptions.MyExceptionHandle.main(MyExceptionHandle.java:12)
After for loop...



Explanation :
As in the above example.... we handled the exception caused by 10/i which is arithematic exception ....because of that after exception occurs the program does not terminate and After the loop...statement also run. 

---------------------------------------------------------------------------------------------------------------------


Read More »