Advanced Java Programming

Image
                          Advanced Java Programming Advanced Java Programming :-   Introduction to advance java   As most of us already know that if we want to make normal applications it  can be easily built using core Java concepts. But, when it we need to develop web applications, advanced Java fundamentals, like JSP, Servlets, JDBC etc. needed, so to add capabilities and features of the application advance java is essential for developers. Through the motive of this blog is to explain about Advanced Java, I will be giving you a complete insight into the fundamental concepts of Advance Java. Figure - 1.2 If you want to see complete video on this please  have a look the video below.                              Learn with Resh u Advanced Java Programming Course ...

Throw keywords in Exception Handling in Java

 Throw keywords in Exception Handling in Java



Java throw keyword

The Java throw keyword is used to explicitly throw an exception.
We can throw either checked or uncheked exception in java by throw keyword. The throw keyword is mainly used to throw custom exception. We will see custom exceptions later.
The syntax of java throw keyword is given below.





  1.  
  2.  throw exception;  



Let's see the example of throw IOException.


  1.  
  2. throw new IOException("sorry device error);  




Example 

Suppose You have a Voting system and the condition to vote is age should be greater then 18 so Code vill be like below.

Example

package com.exception.examples;
import java.util.Scanner;
public class VotingSystem {
public void vote() throws AgeException
{ Scanner s = new Scanner(System.in);
  System.out.println("Enter your Age  :"); 
   int age = s.nextInt();
                     if(age < 18) {
                         throw new AgeException("Age is not vailid"); 
   }else {
    System.out.println("your Age is eligible for
                                voting");  
         }
          System.out.println("hello ");  
}
}


Here the program throw  Custom Exception AgeException which is defile like below


Example

package com.exception.examples;
public class AgeException extends RuntimeException{
public AgeException(String msg){
super(msg);
}
}
public class Caller {
public static void main(String args[]){
  VotingSystem vs = new VotingSystem();
   vs.vote();
}
}

Output:

Exception in thread main com.exception.examples.AgeException :Age is not vailid






Thanks For Watching!!!

Comments

Popular posts from this blog

Learn Complete OOPS Concepts in one Project

Spring Core

Polymorphism in Java Method Overriding and Method OverLoading in Java