Nested try

Nested try

Each time when i execute this code in eclipse i get different answers pl help me with this code

class Demo
{
static void nestedTry(String args[])
{
try 
{
 int a = Integer.parseInt(args[0]);
 int b = Integer.parseInt(args[1]);
System.out.println(a/b);
 } 
catch (ArithmeticException e)
{
System.out.println("Div by zero error!");
 }
 finally
 {
     System.out.println("Finally");
 }
}
public static void main(String args[])
{
     try {
     nestedTry(args);
     } 
     catch (ArithmeticException e) 
     {
    System.out.println("Need 2 parameters!");
     }
    }
}
View Answers

March 14, 2012 at 3:51 PM

Definitely, you will get different outputs. The output depends on your input values. After compiling the code, if you run the code with the following inputs:

java Demo 2 3, it will give output 0 , the try block of method is called which divides one value from other 2/3 whose result is actually a decimal value which is converted to int and gives 0.

java Demo 2 2, it will give output 1 , the try block of method is called which divides one value from other 2/2 and gives 0.

java Demo 3 2, it will give output 0 , the try block of method is called which divides one value from other 3/2 whose result is actually a decimal value which is converted to int and gives 1.

java Demo 2 0, it will give exception, the try block of method is called which passes the condition to catch block as an exception (cannot divided by 0) occurs and display the error 'Div by zero error!'.

If you will not input any number, 
java Demo, it will return ArrayIndexOutOfException: 0.

If you will input only one number,
java Demo 2 it will return ArrayIndexOutOfException: 1.

Finally always execute in any case.

For more information, visit the following link:

Nested Try Example









Related Tutorials/Questions & Answers:
Nested try
Nested try  Hi, In eclipse without providing input i am getting... versa.pl explain me class Demo { static void nestedTry(String args[]) { try... static void main(String args[]) { try { nestedTry(args); } catch
Nested try
, visit the following link: Nested Try Example...Nested try  Each time when i execute this code in eclipse i get...(String args[]) { try { int a = Integer.parseInt(args[0]); int b
Advertisements
Nested Try-Catch Blocks
Nested Try-Catch Blocks       In Java we can have nested try and catch blocks. It means... of the catch statements succeeds, or until all of the nested try statements
Nested try catch
Nested try catch      ... be written in the try block.  If the exceptions occurs at that particular block then it will be catch by the catch block. We can have more than one try/catch
try catch method in java
try catch method in java  try catch method in java - when and how should i use the try and catch method in Java ?   Please visit...://www.roseindia.net/java/exceptions/nested-try.shtml
Try it Editor
Try it Editor  Hello sir...actually i want to add an html,css & js editor like in w3 school try it editor 1.5....can you tell me how i can add it..pllz plzzz rppy soon
try catch
try catch  why following code gives compile time error.please reply...=1;i<=3;i++) { System.out.println(i); try...); try { Thread.sleep(1000
Nested class
Nested class  What is nested class?  when a class is defined within another class then such a class is called a nested class. Nested... nested classes are: It is a way of logically grouping classes that are only used
nested loops
nested loops  please tell input to get the output: 12345 23456 34567 45678
Try Ruby
Try Ruby       Try out Ruby code in this site, by typing the ruby code then see the result. Read full DescriptionADS_TO_REPLACE_1
Nested Clases
Nested Clases  public class Nestclasses { a obj=new a(); public..."; System.out.println("hellwo in nested class"+i+ch); } } } class Outer..."); } } } /*Q:-Here I innner Class Constructor but its not call why i
program to create nested tables
program to create nested tables  program to create nested tables
nested for-loop
nested for-loop  hi.write a program that will prompt the user for a positive integer(num) and then display two triangles with num number of lines to represent the following patterns of asterisks. the input num must be validated
Nested classes: Examples and tutorials
Nested classes: Examples and tutorials       Nested classes Here is another advantage of the Java... within another class, such class is called a nested class. Inner classes can
nested for loops
nested for loops  Hello all, I have created a program(gui) that allows a user to input time and minutes in textfields and then calculates... that the problem lies with my nested for loop which calculates all information
The nested class - Java Beginners
The nested class  Hi, What is nested class in Java? What is the benefits of nested class? Write examples of nested class. Thanks   .../java/n/java-nested-class.shtml Thanks
Inner Nested Classes
Inner Nested Classes       Non-static nested classes are slightly different from static nested classes, a non-static nested class is actually associated
ModuleNotFoundError: No module named 'Nested'
ModuleNotFoundError: No module named 'Nested'  Hi, My Python... 'Nested' How to remove the ModuleNotFoundError: No module named 'Nested'... to install padas library. You can install Nested python with following command
ModuleNotFoundError: No module named 'try'
ModuleNotFoundError: No module named 'try'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'try' How to remove the ModuleNotFoundError: No module named 'try' error
java try catch
java try catch  try{ return 1; }catch(exception e){ return 2; } finally{ Return 3; } What is the out put if any exception occurred
GROUP BY IN NESTED QUERY
GROUP BY IN NESTED QUERY  Hello Every One, Query to use GROUP BY CLAUSE IN NESTED QUERY SELECT STATEMENT in mysql database.If u know please help me.thanks
try and finally block
try and finally block  hello, If I write System.exit (0); at the end of the try block,ADS_TO_REPLACE_1 will the finally block still execute?   hii, if we use ADS_TO_REPLACE_2 System.exit (0); statement any
Try and catch in Exception Handling.
Try and catch in Exception Handling.  How can we use try and catch...; try{ x=a/b; }catch (Exception er.... Then to perform exception handling, we have used try and catch block where we have
Java nested class example
Java nested class example  Give me any example of Nested Class.   Nested Class: Class defined within another class is called nested class. Nested class is divided into two parts- 1.Static 2.Non-static Static
Need help with nested queries
Need help with nested queries  Hello, Table1 has "id", "votes" columns. Table2 has "id", "Name" column. I need to retrieve Name and its corresponging Votes. I tried with nested queries but its confusing. Can anyone please help
Nested and Inner classes
Nested and Inner classes  What is the significance of Inner Classes and Static Inner Classes? or Why are nested classes used?   Hi Friend... a nested class. Inner class is a non static class declared inside another class.Non
Nested If Statement
Nested If Statement       In this section you will study about the Nested-if Statement in jsp.ADS_TO_REPLACE_1 Nested If statement means to use the if statement inside the other
Version of com.lambdista>try dependency
List of Version of com.lambdista>try dependency
Version of com.talanlabs>try dependency
List of Version of com.talanlabs>try dependency
Using Nested loops
Using Nested loops  How to use nested loops in java when I want to print the 10 multiples of numbers 2 to 15(in multiplication table)   public class MultiplicationTable{ public static void main(String[] args) { int
nested exception is java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource
nested exception is java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource  Hi, I am getting the following error while starting a web application: nested exception
Version of struts>struts-nested dependency
List of Version of struts>struts-nested dependency
java: try finally blocks execution
java: try finally blocks execution  java: try finally blocks execution
ModuleNotFoundError: No module named 'drf-nested'
ModuleNotFoundError: No module named 'drf-nested'  Hi, My Python... 'drf-nested' How to remove the ModuleNotFoundError: No module named 'drf-nested' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'easy_nested'
ModuleNotFoundError: No module named 'easy_nested'  Hi, My Python... 'easy_nested' How to remove the ModuleNotFoundError: No module named 'easy_nested' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'list_nested'
ModuleNotFoundError: No module named 'list_nested'  Hi, My Python... 'list_nested' How to remove the ModuleNotFoundError: No module named 'list_nested' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'nested_again'
ModuleNotFoundError: No module named 'nested_again'  Hi, My Python... 'nested_again' How to remove the ModuleNotFoundError: No module named 'nested_again' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'nested-cv'
ModuleNotFoundError: No module named 'nested-cv'  Hi, My Python... 'nested-cv' How to remove the ModuleNotFoundError: No module named 'nested... have to install padas library. You can install nested-cv python with following
ModuleNotFoundError: No module named 'nested-diff'
ModuleNotFoundError: No module named 'nested-diff'  Hi, My Python... 'nested-diff' How to remove the ModuleNotFoundError: No module named 'nested-diff' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'Nested-List'
ModuleNotFoundError: No module named 'Nested-List'  Hi, My Python... 'Nested-List' How to remove the ModuleNotFoundError: No module named 'Nested-List' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'nested-printer'
ModuleNotFoundError: No module named 'nested-printer'  Hi, My... named 'nested-printer' How to remove the ModuleNotFoundError: No module named 'nested-printer' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'nested_namespace'
ModuleNotFoundError: No module named 'nested_namespace'  Hi, My... named 'nested_namespace' How to remove the ModuleNotFoundError: No module named 'nested_namespace' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'nested_print'
ModuleNotFoundError: No module named 'nested_print'  Hi, My Python... 'nested_print' How to remove the ModuleNotFoundError: No module named 'nested_print' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'nested_structures'
ModuleNotFoundError: No module named 'nested_structures'  Hi, My... named 'nested_structures' How to remove the ModuleNotFoundError: No module named 'nested_structures' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'nester_nested'
ModuleNotFoundError: No module named 'nester_nested'  Hi, My... 'nester_nested' How to remove the ModuleNotFoundError: No module named 'nester_nested' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'Snester_nested'
ModuleNotFoundError: No module named 'Snester_nested'  Hi, My... named 'Snester_nested' How to remove the ModuleNotFoundError: No module named 'Snester_nested' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'bamboo-nested'
ModuleNotFoundError: No module named 'bamboo-nested'  Hi, My... 'bamboo-nested' How to remove the ModuleNotFoundError: No module named 'bamboo-nested' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'drf-nested'
ModuleNotFoundError: No module named 'drf-nested'  Hi, My Python... 'drf-nested' How to remove the ModuleNotFoundError: No module named 'drf-nested' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'drf-nested'
ModuleNotFoundError: No module named 'drf-nested'  Hi, My Python... 'drf-nested' How to remove the ModuleNotFoundError: No module named 'drf-nested' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'easy_nested'
ModuleNotFoundError: No module named 'easy_nested'  Hi, My Python... 'easy_nested' How to remove the ModuleNotFoundError: No module named 'easy_nested' error? Thanks   Hi, In your python environment

Ads