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

Java Customized Exceptions / Chapter 11 / Exception Handling

Java Customized Exceptions


Sometimes to fulfill the programming requirement we can define our own exceptions such type of exceptions are called customized exceptions.

Let's take an example of customized exceptions.
import java.util.*;
class TooYoungException extends RuntimeException
{
TooYoungException (String s)
{
super(s);
}
}
class TooOldException extends RuntimeException
{
TooOldException (String s)
{
super(s);
}
}
class CustomizeException
{
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the age ");
int age=sc.nextInt();
if(age>60)
{
throw new TooOldException("Your age is already crossed the marriage age...
try In next birth... ");
}
else if(age<18)
{
throw new TooYoungException("Please wait some time...
You will get your best match soon... ");
}
else
{
System.out.println("Congrats!!!You are eligible for marriage..
you will get details of your match by email!!");
}
}
}

Output is:-

java throws keyword /Chapter 10 / Exception Handling

java throws

Java throws keyword


We can use throws keyword to delegate the responsibility of exception handling to the caller. It may be another method or JVM. After then caller method is responsible to handle that exception.

example:-
class Test
{
public static void main(String [] args) throws InterruptedException
{
Thread.sleep(5000);
}
}


java throw keyword /Chapter 9 /Exception Handling

java throw

java throw keyword:-


throw keyword used to create our own exception object explicitly and we can handover this (our own created) exception object to JVM manually.


example:-java throw

The main objective of throw keyword is to hand over our created exception object to JVM manually.


Let's take two example where the output will be same.

Some Possible Combinations of try catch finally /Chapter 8 / Exception Handling

try catch finally


Some possible combinations of try catch finally


try 
{

}
catch(Exception e)
{

}

The above combination is valid.

—————————————————————————————————————————————
try
{

}
catch(ArithmeticException e)
{

}
catch(Exception e)
{

}

The above combination is valid.

—————————————————————————————————————————————
try
{

}
catch(ArithmeticException e)
{

}
catch(ArithmeticException e)
{

}

The above combination is invalid.And we will get Compile Time Error.

Difference Between final,finally,finalize /Chapter 7 / Exception Handling

Difference Between final, finally, finalize


final is a modifier applicable for method, classes & variables.


1.final method:-


Whatever method parent class has by default available to the child class through inheritance.

If the child class is not satisfied with the parent method implementation than the child is allowed to redefine that method based on its requirement, this process is called "Overriding".


But,

If the parent class method declared as final then we can't override that method in child class. Because it's implementation is final.


2. final class:-

If a class declared as final we can't extend the functionality of that class so that we can not create the child class for that class. That's why inheritance is impossible for final classes.


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

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

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: -

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com