Do-while and Exception Hadnling

exception handling in while loop java
try-catch in while loop java
do-while loop java
do while loop try catch java
how to handle error in java
try-catch loop java
java do-while loop with user input
do-while loop java - stack overflow
package learnjava;

import java.util.Scanner;

public class exception {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int x =1;
    do {
        try {
            System.out.println("Input first number : ");
            int n1 = input.nextInt();
            System.out.println("Input second number : ");
            int n2 = input.nextInt();
            int sum = n1 / n2;
            System.out.println("result = "+sum);
            x = 2;

        } catch (Exception e) {
            System.out.println("Error");

        }

    } while (x == 1);

     }
 }

I watched java tutorials in thenewboston..... I want ask you...why x=2...?can you explain why?...thanks


This a code doesn't approach it's own concept well. Nonetheless Do-While is a exit control loop and is keeps on executing the loop till x is not equal to any number except 1. That is if there is any change in x apart from being 'x = 1'then the loop would break as in the loop will stop functioning. Here we are using a try catch to prevent any number being divided by zero and we use x=2 to execute the loop once and exit.

Anyway this is concept builder, but this is not how one would approach this methodology.

How to reiterate a do while loop after an exception is caught, run , to reiterate a do while loop after an exception is caught, run time error handling is caught, and am having difficulty figuring how to handle the exception part Exception handling uses the try, catch, and finally keywords to try actions that may not succeed, to handle failures when you decide that it is reasonable to do so, and to clean up resources afterward. Exceptions can be generated by the common language runtime (CLR), by .NET or third-party libraries, or by application code.


do-while loop executes once without checking the condition. In this case the condition to continue while loop is x == 1 . That is because you have set x = 1 in your code above.

While executing prior to checking the condition, using the scanner, you read 2 integers and done some operations on them and printed the result. As the logic expects flow to stop here (that is just reading 2 integers and printing result), x has been set to 2 that is x = 2.

Now answering your question why x = 2. It can be anything other than 1 which will terminate the while loop and not allow any further iterations.

What's an Exception and Why Do I Care?, conditions requiring special processing – often changing the normal flow of program execution. It is provided by specialized programming language constructs or computer hardware mechanisms. You can implement the handling for one or more exception types within a catch block. As you can see in the following code snippet, the catch clause gets the exception as a parameter. As you can see in the following code snippet, the catch clause gets the exception as a parameter.


this code do division if you inter the n2 value to 0, exception happened and printed the error and the x=1 value was not changed the while condition is true and your code running again and want the input. if your input was true (n2!=0) your operation done and the x value changed to two x=2. when the while condition checked the result is false and break the do while loop. I write some comment in your code.

package learnjava; 
import java.util.Scanner;
public class exception {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int x =1;
    do {
        try {
            System.out.println("Input first number : ");
            int n1 = input.nextInt();
            System.out.println("Input second number : ");
            int n2 = input.nextInt();
            int sum = n1 / n2;//if your n2 input was 0 exception happened and go to catch block.
            System.out.println("result = "+sum);
            x = 2;// if one time your code execute without exception the x changed to 2. if exception happened your code execute again.

        } catch (Exception e) {
            System.out.println("Error");

        }

    } while (x == 1);

     }
 }

What is the purpose of Exception Handling?, You'd still have to put it in a loop: #include <ios> #include <iostream> #include <​limits> using namespace std; int main() { bool done = false;  A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero. Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is built upon three keywords: try, catch, and throw.


Its simple,

The do while works this way.

First the do part gets executes, if the condition in the while loop is satisfied, the do loop executes again. This will go on, till the condition in the while loop is not satisfied.

 do {
       //work is done. 
       //now lets us change the condition so that we can get out of this do-while loop. 
       // this condition is nothing but setting x equal to nay value other than 1. It can be 2 or 10000 or -1.
    } while (x == 1);

So here once the functionlity of the do loop is satisfied, the x==2 is done. This makes sure that the condition in while is not satisfied and hence the code exists do-while loop.

Java: Try-Catch statement within a Do-While loop, Java do-while loop is used to execute a block of statements continuously until the 1.1 do while java flow diagram; 1.2 Java do-while loop example; 1.3 do while loop must return a boolean value, otherwise, it will throw compile-time error. Exception handling was subsequently widely adopted by many programming languages from the 1980s onward. PL/I used dynamically scoped exceptions, however more recent languages use lexically scoped exceptions. PL/I exception handling included events that are not errors, e.g., attention, end-of-file, modification of listed variables.


Exceptions in Java, 1- First example uses do while loop to print numbers from 1 to 10. 1. 2. 3. 4. 5. 6. 7. The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.. In this page, we will learn about Java exceptions, its type and the difference between checked and unchecked exceptions.


c++, while (x < 10) { x++; }. Here, { x++; } is the block statement. Important: JavaScript before ECMAScript2015 (6th edition) does not have block  Exception handling ensures that the flow of the program doesn’t break when an exception occurs. For example, if a program has bunch of statements and an exception occurs mid way after executing certain statements then the statements after the exception will not execute and the program will terminate abruptly.


Java do while loop, You are using a try-catch statement improperly. the statements are meant to be used when using prewritten classes that might throw an exception. Exception Handling mechanism follows a flow which is depicted in the below figure. But if an exception is not handled, it may lead to a system failure. That is why handling an exception is very important. You may also go through this recording of Java Exception Handling where you can understand the topics in a detailed manner with examples.