The Artima Developer Community
Sponsored Link

Java Buzz Forum
try catch finally 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.
try catch finally in java Posted: Jul 25, 2015 7:24 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: try catch finally 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

try block:

  • The functionality of try keyword is to identify an exception object.
  • And catch that exception object and transfer the control along with the identified exception object to the catch block by suspending the execution of the try block.
  • All the statements which are proven to generate exceptions should be place in try block.
Syntax of try block:

try
{
//try block
//keep those statements which may
//throw run time exceptions
}

catch Block:
  •  The functionality of catch block is to receive the exception class object that has been send by the "try".
  • And catch that exception class object and assigns that exception class object to the reference of the corresponding exception class defined in the catch block.
  • And handle the exception that has been identified by "try".
  • "try"  block identifies an exception and catch block handles the identified exception.
Syntax:

catch(Exception e)
{
//catch block.
//one argument of type java.lang.Exception
//catches the exceptions thrown by try block
}

finally block:


  • finally blocks are the blocks which are going to get executed compulsorily irrespective of exceptions.
  • finally blocks are optional blocks.
Syntax:

finally
{
//This is the finally block.
}

Example Program:
  1. package com.instanceofjava;
  2. public class MyException {
  3.  
  4.  public static void main(String a[]){
  5.  
  6.   try{         
  7. int i = 10/0;             
  8.   System.out.println("This statement won't executed");      
  9.  } 
  10. catch(Exception ex){
  11.  
  12.   System.out.println("This block is executed immediately after an exception is thrown");
  13.  
  14.   }
  15.  finally{
  16. System.out.println("This block is always executed");}
  17.  }
  18.  
  19. }
Output:
  1. This block is executed immediately after an exception is thrown
  2. This block is always executed.

Read: try catch finally in java

Topic: CQRS and Event Sourcing for dummies Previous Topic   Next Topic Topic: How to calculate Maximum and minimum in Java? Beginner Tutorial

Sponsored Links



Google
  Web Artima.com   

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