Try-catch training

I'm a Java beginner, so please carry me

static int load = 100;
static int greet;

public void loadDeduct(int cLoad, int c){
    int balance;
    balance = cLoad - 7;
    System.out.println("Your balance: " + balance);
}

public void loadDeduct(int tLoad){
    int balance;
    balance = tLoad - 1;
    System.out.println("Your balance is: " + balance);
}

public static void main (String [] args){
    int choice;
    Scanner scan = new Scanner(System.in);

    System.out.println("I'm a cellphone, what do you want to do?");
    System.out.println("Press 1 to send SMS / Press 2 to Call");

    choice = scan.nextInt();

    CellphoneLoad N95 = new CellphoneLoad();

    if (choice == 1){
        N95.loadDeduct(load);
    }else if (choice == 2){
        N95.loadDeduct(load, greet);
    }else{
        System.out.println("Invalid Option!!!");
    }

How to implement exception handling using this program? I'm not quite sure how to use a blocking block, since we have not yet been taught all the things of exceptions. It was just an exercise that we asked to do. I want to replace if else statements with try-catch blocks ... is this possible?

+3
source share
7 answers

One of the important principles for considering exceptions in Java is the presence of two types: 1. Runtime 2. Typical / explicit (due to lack of a better word)

, , , , .

/ , - .

, , .

, .

+2

. , . , .

+1

, , :

scan.nextInt();

JavaDocs :

  • InputMismatchException ( )
  • NoSuchElementException ( )
  • IllegalStateException ( )

, , , :

try {
    choice = scan.nextInt();
} 
catch (InputMismatchException e) {
  System.out.println(e.getMessage());
}
catch (NoSuchElementException e) {
  System.out.println(e.getMessage());
}
catch (IllegalStateException e) {
  System.out.println(e.getMessage());
}

, , "catch" , , / .

"" , , , - , , , ( ..) Java).

, "if", "switch":

switch (choice) {
    case 1:  N95.loadDeduct(load);
             break;
    case 2:  N95.loadDeduct(load, greet);
             break;
    default: System.out.println("Invalid Option!!!");
}
+1

Scanner.nextInt() . API , .

, , "one" 1, InputMismatchException.

try-catch , :

try
{
    Integer.parseInt("one");      // Statement that can cause an exception.
}
catch (NumberFormatException e)   // Specify which exception to catch.
{
    // Code to handle the NumberFormatException.
}

: Java. , Catching and Handling Exceptions.

+1

. - :

public static void main (String [] args){

.....

try{
 handleUserChoice(choice);//new method
}
catch(InvalidChoiceException e){
 System.out.println("Invalid Option!!!");
}
}
+1

. , . , , . try.. catch . ", ":

public class TryTry {

    public void doIt() throws Exception {
       System.err.println("In the doIt method.");
       throw new Exception("Hello there!");
    }

    public static void main(String[] argv){
       TryTry t = new TryTry();
       // here we'll catch it.
       try {
           System.err.println("About to call doIt().");
          t.doIt();
       } catch (Exception e) {  // e now has your exception object
          System.err.println("In the exception handler.");
          System.err.println("Exception says: "+ e);
       }
    }
}

throw , . , , tr { ... }, catch.

:

javac TryTry.java && java TryTry
About to call doIt().
In the doIt method.
In the exception handler.
Exception says: java.lang.Exception: Hello there!
0
source

I see no reason to use exceptions instead of the if-else block. you can try using the switch statement, it will look better.

you should use exceptions to handle errors that may occur inside loadDeduct methods. then you would surround the lines calling N95.loadDeduct with a try-catch block to write what would happen if loadDeduct did wrong.

0
source

Source: https://habr.com/ru/post/1707920/


All Articles