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 8284141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:59:09+00:00 2026-06-08T10:59:09+00:00

For example: try { SomeObject someObject = new SomeObject(); someObject.dangerousMethod(); } catch(Exception e) {

  • 0

For example:

try
{
    SomeObject someObject = new SomeObject();
    someObject.dangerousMethod();
}
catch(Exception e)
{
}
someObject.anotherMethod(); //can't access someObject!

But you can declare it before the try/catch block and then it works fine:

SomeObject someObject;
try
{
    someObject = new SomeObject();
    someObject.dangerousMethod();
}
catch(Exception e)
{
}
someObject.anotherMethod(); //works fine

I’m just wondering the design reason for this. Why are Objects created within the try/catch block not in scope with the rest of the method? Maybe I’m not understanding deep down how a try/catch works besides just watching for Exceptions thrown.

  • 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-06-08T10:59:11+00:00Added an answer on June 8, 2026 at 10:59 am

    Why are Objects created within the try/catch block not in scope with the rest of the method?

    They are. Variables declared within the try/catch block are not in scope in the containing block, for the same reason that all other variable declarations are local to the scope in which they occur: That’s how the specification defines it. 🙂 (More below, including a reply to your comment.)

    Here’s an object created within a try/catch which is accessible outside of it:

    SomeObject someObject = null;
    try
    {
        someObject = new SomeObject();
        someObject.dangerousMethod();
    }
    catch(Exception e)
    {
    }
    someObject.anotherMethod(); // This is fine -- unless the SomeObject
                                // constructor threw the exception, in which
                                // case someObject will be null
    

    Note the difference. Where the variable is declared defines the scope in which it exists, not where the object was created.

    But based on the method names and such above, the more useful structure for that would be:

    SomeObject someObject = new SomeObject();
    try
    {
        someObject.dangerousMethod();
    }
    catch(Exception e)
    {
    }
    someObject.anotherMethod();
    

    Re your comment:

    I guess I’m confused as to why another scope has even been created for a try/catch block.

    In Java, all blocks create scope. The body of an if, the body of an else, of a while, etc. — they all create a new, nested variable scope:

    if (foo) {
        SomeObject bar = new SomeObject();
    }
    bar.doSomething(); // <== Compilation error, `bar` is not defined
    

    (In fact, even a block without any control structure creates one.)

    And if you think about it, it makes sense: Some blocks are conditional, like the one defining the body of an if or while. In the above if, bar may or may not have been declared (depending on the value of foo), which makes no sense because of course the compiler has no concept of the runtime value of foo. So probably for consistency, the designers of Java went with having all blocks create a new nested scope. (The designer of JavaScript went the other way — there is no block scope at all, yet, though it’s being added — and that approach also confuses people.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example: @try { // 1) do bad stuff that can throw an exception... //
In this example: try { this.myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), queryStringKeyValue); } catch (Exception) { this.myEnum
How does a return statement inside a try/catch block work? function example() { try
how we can return some value in try-catch for example try { return abc;
I want to use a try-catch block to handle two cases: a specific exception,
How can I know during runtime that my code threw a Warning? example try
example i try to change cell.accessoryView from picture1 to picture2 by fadein but my
I try to use doctest from example from http://docs.python.org/library/doctest.html But when I run python
Can two or more 'try/catch' statements share a 'finally' statement in C#? I am
If I create a stream inside of the try block and an exception is

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.