简体   繁体   English

这个 try catch 代码(Java)有什么问题?

[英]What's wrong with this try catch code (Java)?

Been 2 hours I'm trying to compile my code, doesn't seem to work... Check out which line gives me a msg in the code has a comment.已经 2 个小时了,我正在尝试编译我的代码,但似乎没有用...查看代码中哪一行给了我一个 msg 有注释。

Does the Try and catch isolate the scoop of a variable? Try 和 catch 是否隔离了变量的勺子?

import java.io.*;
public class Tp3Partie2 {

    public static void main (String[] params) {
        int cmd;
        FileReader fEntree;
        FileReader fEntree2;
        BufferedReader entree;
        Cars [] tableau;
        try {
            fEntree = new FileReader( "test.txt" );
            fEntree2 = new FileReader( "test.txt" );
            LineNumberReader nbrLigne = new LineNumberReader( fEntree );
            nbrLigne.skip(Long.MAX_VALUE);
            int nbrLigneFichier = nbrLigne.getLineNumber();
            nbrLigneFichier = nbrLigneFichier + 1;
            entree = new BufferedReader( fEntree2 );
            tableau = new Cars[nbrLigneFichier/5];
            System.out.println( tableau.length );
            for(int i=0;i<nbrLigneFichier/5;i++) {
                String type = entree.readLine();
                type.trim();
                int posType = Cars.obtenirCode(type, 't');

                String couleur = entree.readLine();
                couleur.trim();
                int posCouleur = Cars.obtenirCode(couleur, 'c');

                int pointure = Integer.parseInt(entree.readLine());
                double prix = Double.parseDouble(entree.readLine());
                boolean neuf = Boolean.parseBoolean(entree.readLine());
                System.out.println(i);
                tableau[i] = new Cars(posType,posCouleur,pointure,prix,neuf);
                tableau[i].toString();
            }
            entree.close();
            nbrLigne.close();
        } catch ( IOException e ) {
            System.out.println( MessagesTp3.FICHIER_EXISTE_PAS );
        }

        do {            
            System.out.println( MessagesTp3.AJOUTER_PAIRE );
            System.out.println( MessagesTp3.SUPPRIMER_PAIRE );
            System.out.println( MessagesTp3.AFFICHER_PAIRES );
            System.out.println( MessagesTp3.QUITTER  );
            System.out.println( MessagesTp3.VOTRE_CHOIX ); 
            cmd = Clavier.lireInt();

            switch (cmd) {
                case 1:
                break;
                case 2:
                break;
                case 3:
                System.out.println( MessagesTp3.LISTE_CHAUSSURES );                
                for(int i=0;i<tableau.length;i++) { // This line gives me a msg of "variable tableau might not been initialized"
                    tableau[i].toString();
                }        
                break;
                case 9:
                break;
                default:
                System.out.println( MessagesTp3.CHOIX_NONVALIDE );
                break;
            }

        } while ( !(cmd == 9) );

        System.out.println( MessagesTp3.FIN_PROGRAMME );
    } // main

    public static void enregistrerPaires () {
    }

    public static void ajouterPaires () {
    }

    public static void suprimerPaires () {
    }
}

Actually try and catch does not limit the scope, but what the compiler is saying is that there might have been an exception thrown before initializing the variable inside the try catch block.实际上 try 和 catch 并没有限制作用域,但是编译器所说的是在 try catch 块内初始化变量之前可能已经抛出了异常。

In that case the exception is thrown and caught, the variable is not initialized and then the code would continue.在这种情况下,异常被抛出并被捕获,变量不会被初始化,然后代码将继续。 But further on, we get to the line with compilation error, where the variable would not have been initialized.但更进一步,我们会遇到编译错误,变量不会被初始化。

The compiler detects this possibility and throws an error, the solution is for example to initialise the variable with the empty array:编译器检测到这种可能性并抛出错误,解决方案是例如使用空数组初始化变量:

Cars [] tableau = {};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM