Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 718411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:31:24+00:00 2026-05-14T05:31:24+00:00

The problem drives me to the big try-catch loops. I want smaller. So how

  • 0

The problem drives me to the big try-catch loops. I want smaller. So how to access assignments in the loops?

$ javac TestInit2.java 
TestInit2.java:13: variable unknown might not have been initialized
  System.out.println(unknown);
                     ^
1 error

Code

import java.util.*;
import java.io.*;

public class TestInit2 {

 public static void main(String[] args){
  String unknown;
  try{
   unknown="cannot see me, why?";
  }catch(Exception e){
   e.printStackTrace();
  }
  System.out.println(unknown);
 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:31:25+00:00Added an answer on May 14, 2026 at 5:31 am

    The compiler is stopping you from doing something that is most likely a mistake, since after your try-catch block, you would probably assume that the variable is initialized. If an exception is thrown, however, it will not be initialized.

    You will need to assign the variable to something before using it. It is, however, possible to just assign it to null, if you want it to be null if the assignment fails.

    So, if you want the variable to be null if the assignment fails, try this:

        String unknown = null;
        try{
            unknown="cannot see me, why?";
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println(unknown);
    

    If you want to set the variable to something else if an exception is caught, try this:

        String unknown;
        try{
            unknown="cannot see me, why?";
        }catch(Exception e){
            e.printStackTrace();
            unknown = "exception caught";
        }
        System.out.println(unknown);        
    

    Also, if it doesn’t make sense to proceed with the execution of your method if the assignment fails, you might want to consider either returning from the catch block, or throwing another exception which would be caught by the caller. For example:

        String unknown;
        try{
            unknown="cannot see me, why?";
        }catch(Exception e){
            e.printStackTrace();
            //return; // if you just want to give up with this method, but not bother breaking the flow of the caller
            throw new Exception("Uh-oh...", e); // if you want to be sure the caller knows something went wrong
        }
        System.out.println(unknown);   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a litle big problem with java heap memory I'm trying to migrate from
This problem drives me nuts, because the max_execution_time in the php.ini and in the
Here's the problem. I've got two different technologies needing drivers to access a PostgreSQL
I have a big problem. At present I am accessing a serial port via
I've got a big project written in PHP and Javascript. The problem is that
I have a big problem with sessions. I drive to create an application around
This problem drives me crazy. I miss some basic but very important knowledge about
I've been running up against a problem with Java Swing + my Wacom Graphire
I have this problem that drives me mad, so I am here to ask
Problem: during execution, instances of classes that derives from System.Workflow.ComponentModel.Activity is serialized by the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.