title: Java how to access a variable outside the try-catch block
body: <p>I am a beginner in java, and was playing around with try-catch block.However, I am not able to get the variable outside the try-catch block.</p>
  
  <p>The following code works.</p>
  
  <pre><code>class factorial{
  public static void main(String[] args){
      try {
           int num = Integer.parseInt(args[0]);
           System.out.println(num );
      }
      catch(Exception e){
          System.out.println(e+" Cannot convert arg to int, exiting..");
      }
  
  
  }
  }
  </code></pre>
  
  <p>But the following doesn't works.</p>
  
  <pre><code>class factorial{
  public static void main(String[] args){
      try {
           int num = Integer.parseInt(args[0]);
      }
      catch(Exception e){
          System.out.println(e+" Cannot convert arg to int, exiting..");
      }
       System.out.println(num );
  }
  }
  </code></pre>
  
  <p>Tried the following as well</p>
  
  <pre><code>class factorial{
  public static void main(String[] args){
      int num;
      try {
           num = Integer.parseInt(args[0]);
      }
      catch(Exception e){
          System.out.println(e+" Cannot convert arg to int, exiting..");
      }
       System.out.println(num );
  
  }
  }
  </code></pre>
  
  <p>But the error says <code>The local variable num may not have been initialized</code></p>
  
  <p>How can I get rid of this error?</p>