The Artima Developer Community
Sponsored Link

Java Buzz Forum
throw keyword in java with example

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
throw keyword in java with example Posted: Apr 25, 2016 3:48 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: throw keyword in java with example
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
Throw keyword in java

  • Throw keyword is used to throw exception manually.
  • Whenever it is required to suspend the execution of the functionality based on the user defined logical error condition we will use this throw keyword to throw exception.
  • So we need to handle these exceptions also using try catch blocks.



 Java simple example Program to explain use of throw keyword in java


  1. package exceptions;
  2.  
  3. public class ThrowKeyword {
  4.  
  5.     
  6. public static void main(String[] args) {
  7.  
  8. try {
  9.             
  10.    throw new ArithmeticException();
  11.             
  12.             
  13. } catch (Exception e) {
  14.  
  15.    System.out.println(e);
  16.    e.printStackTrace();
  17. }
  18.  
  19. }
  20.  
  21. }

Output:



  1. java.lang.ArithmeticException
  2. java.lang.ArithmeticException
  3.     at exceptions.ThrowKeyword.main(ThrowKeyword.java:11)

Rules to use "throw" keyword in java
  • throw keyword must follow Throwable type of object.
  • It must be used only in method logic.
  • Since it is a transfer statement , we can not place statements after throw statement. It leads to compile time error Unreachable code



throw keyword in java


  •  We can throw user defined exception using throw keyword.

  1. //Custom exception
  2. package com.instanceofjavaforus;
  3. public class InvalidAgeException extends Exception {
  4.  InvaidAgeException(String msg){
  5.  super(msg);
  6.  }
  7. }
     

  1. package com.exceptions;
  2. public class ThrowDemo {

  3. public boolean isValidForVote(int age){
  4.  
  5.  try{
  6.    if(age<18){
  7.   throw new InvalidAgeException ("Invalid age for voting");
  8. }
  9.  }catch(Exception e){
  10.  System.out.println(e);
  11.  }
  12.   return false;
  13.  }
  14.  
  15. public static void main(String agrs[]){
  16.  
  17.  ThrowDemo obj= new ThrowDemo();
  18.    obj.isValidForVote(17);
  19.  
  20.   }
  21. }
 Output:


  1.  exceptions.InvalidAgeException: Invalid age for voting
  • We can throw predefined exceptions using throw keyword


  1. package com.instanceofjava;
  2.  
  3. public class ThrowKeyword{
  4. public void method(){
  5.  
  6.  try{
  7.   
  8.   throw new NullPointerException("Invalid age for voting");
  9. }
  10.  }catch(Exception e){
  11.  System.out.println(e);
  12.  }
  13.  }
  14.  
  15.   public static void main(String agrs[]){
  16.  
  17.  ThrowKeyword obj= new ThrowKeyword();
  18.  
  19.    obj.method();
  20.  
  21.   }
  22. }

 Output:


  1.  java.lang.NullPointerException: Invalid age for voting

Read: throw keyword in java with example

Topic: Spring Security with Spring REST Web-service Previous Topic   Next Topic Topic: Performance vs Reliability: Why Java Apps are Like F1 Cars

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use