The Artima Developer Community
Sponsored Link

Java Buzz Forum
Difference between throw and throws in java

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.
Difference between throw and throws in java Posted: Jul 25, 2015 11:23 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Difference between throw and throws in java
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:
  • throw keyword used to throw user defined exceptions.(we can throw predefined exception too)
  • If we are having our own validations in our code we can use this throw keyword.
  • For Ex: BookNotFoundException, InvalidAgeException (user defined).

Program:
  1. package com.instanceofjava;
  2. public class MyExceptionThrow {
  3.  public static void main(String a[]){
  4.  try{
  5. MyExceptionThrow thr = new MyExceptionThrow();
  6. System.out.println("length of INDU is "+thr.getStringSize("INDU"));
  7. System.out.println("length of SAIDESH is "+thr.getStringSize("SAIDSH"));
  8. System.out.println("length of null string is "+thr.getStringSize(null)); 
  9.  }
  10. catch (Exception ex){
  11.   System.out.println("Exception message: "+ex.getMessage());  
  12.  }
  13.  }
  14.    public int getStringSize(String str) throws Exception{
  15.  if(str == null){
  16.    throw new Exception("String input is null");  
  17.  }
  18.  return str.length();
  19. }
  20. }


Output
length of INDU is 4
length of SAIDESH is 5
Exception message: String input is null

 throws keyword:

  •  The functionality of throws keyword is only to explicitly to mention that the method is proven transfer un handled exceptions to the calling place.

Program:
  1. package com.instanceofjava;
  2. public class ExcpetionDemo {
  3. public static void main(String agrs[]){
  4. try
  5. {
  6. //statements
  7. }
  8. catch(Exception e)
  9. {
  10. System.out.println(e);
  11. }
  12. finally(){compulsorily executable statements
  13. }
  14. }
  15. }

Unreachable Blocks:

  • The block of statements to which the control would never reach under any case can be called as unreachable blocks.
  • Unreachable blocks are not supported by java.
  • Thus catch block mentioned with the reference of  "Exception" class should and must be always last catch block. Because Exception is super class of all exceptions.


Program:

Ads by Roll AroundAd Options
  1. package com.instanceofjava;
  2. public class ExcpetionDemo {
  3. public static void main(String agrs[])
  4. {
  5. try
  6. {
  7. //statements
  8. }
  9. catch(Exception e)
  10. {
  11. System.out.println(e);
  12. }
  13. catch(ArithmeticException e)//unreachable block.. not supported by java. leads to error
  14. System.out.println(e);
  15. }
  16. }

Read: Difference between throw and throws in java

Topic: Iterative or Incremental? Previous Topic   Next Topic Topic: CQRS and Event Sourcing for dummies

Sponsored Links



Google
  Web Artima.com   

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