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

  • Home
  • SEARCH
  • 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 49883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:30:23+00:00 2026-05-10T16:30:23+00:00

In C# and in Java (and possibly other languages as well), variables declared in

  • 0

In C# and in Java (and possibly other languages as well), variables declared in a ‘try’ block are not in scope in the corresponding ‘catch’ or ‘finally’ blocks. For example, the following code does not compile:

try {   String s = 'test';   // (more code...) } catch {   Console.Out.WriteLine(s);  //Java fans: think 'System.out.println' here instead } 

In this code, a compile-time error occurs on the reference to s in the catch block, because s is only in scope in the try block. (In Java, the compile error is ‘s cannot be resolved’; in C#, it’s ‘The name ‘s’ does not exist in the current context’.)

The general solution to this issue seems to be to instead declare variables just before the try block, instead of within the try block:

String s; try {   s = 'test';   // (more code...) } catch {   Console.Out.WriteLine(s);  //Java fans: think 'System.out.println' here instead } 

However, at least to me, (1) this feels like a clunky solution, and (2) it results in the variables having a larger scope than the programmer intended (the entire remainder of the method, instead of only in the context of the try-catch-finally).

My question is, what were/are the rationale(s) behind this language design decision (in Java, in C#, and/or in any other applicable languages)?

  • 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. 2026-05-10T16:30:24+00:00Added an answer on May 10, 2026 at 4:30 pm

    Two things:

    1. Generally, Java has just 2 levels of scope: global and function. But, try/catch is an exception (no pun intended). When an exception is thrown and the exception object gets a variable assigned to it, that object variable is only available within the ‘catch’ section and is destroyed as soon as the catch completes.

    2. (and more importantly). You can’t know where in the try block the exception was thrown. It may have been before your variable was declared. Therefore it is impossible to say what variables will be available for the catch/finally clause. Consider the following case, where scoping is as you suggested:

       try {     throw new ArgumentException('some operation that throws an exception');     string s = 'blah'; } catch (e as ArgumentException) {       Console.Out.WriteLine(s); } 

    This clearly is a problem – when you reach the exception handler, s will not have been declared. Given that catches are meant to handle exceptional circumstances and finallys must execute, being safe and declaring this a problem at compile time is far better than at runtime.

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

Sidebar

Ask A Question

Stats

  • Questions 93k
  • Answers 93k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm sure there's probably a little more efficient way of… May 11, 2026 at 6:39 pm
  • Editorial Team
    Editorial Team added an answer Are you sure that these DLLs are managed-code-dlls? I don't… May 11, 2026 at 6:39 pm
  • Editorial Team
    Editorial Team added an answer Equal hash means equal file, unless someone malicious is messing… May 11, 2026 at 6:39 pm

Related Questions

I have a contract in the offering from a client to develop an intranet
I'm working on a web project that will (hopefully) be available in several languages
Ada , Pascal and many other languages support ranges, a way to subtype integers.
I've been jumping from C# to Java an awful lot and the differences between

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.