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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:11:38+00:00 2026-05-18T08:11:38+00:00

Can I some how make my non-exception objects become ‘throwable’ so that any local

  • 0

Can I some how make my non-exception objects become ‘throwable’ so that any local variables declared in a try block like obj in the try block below

try
{ 
   SomeObject obj = new SomeObject(); 
}catch {}

can cross the scope boundary into the catch{} clause?? I want to use the obj instance in the catch{} clause even if its state is compromised. Is this possible somehow ?

Maybe possible(?) as the C# compiler maybe doing similar (for optimization I’d assume?) and allows the catch clause to receive any System.Object but I can’t use that in VS, for this method:

public void Foo()
{

    try
    {
    }
    catch(Exception ex)
    {
    }

    try
    {
    }
    catch
    {
    }

}

generates this IL:

.method public hidebysig instance void  Foo() cil managed
{
  // Code size       22 (0x16)
  .maxstack  1
  .locals init ([0] class [mscorlib]System.Exception ex)
  IL_0000:  nop
  .try
  {
    IL_0001:  nop
    IL_0002:  nop
    IL_0003:  leave.s    IL_000a
  }  // end .try
  catch [mscorlib]System.Exception 
  {
    IL_0005:  stloc.0
    IL_0006:  nop
    IL_0007:  nop
    IL_0008:  leave.s    IL_000a
  }  // end handler
  IL_000a:  nop
  .try
  {
    IL_000b:  nop
    IL_000c:  nop
    IL_000d:  leave.s    IL_0014
  }  // end .try
  catch [mscorlib]System.Object     /* <-- I want to do like this*/
  {
    IL_000f:  pop
    IL_0010:  nop
    IL_0011:  nop
    IL_0012:  leave.s    IL_0014
  }  // end handler
  IL_0014:  nop
  IL_0015:  ret
} // end of method Class::Foo

Is there a way (managed/unmanged) to ‘cheat’ like that and define a ‘throwable’ behavior for the SomeObject type so that I can reserve my right to inherit from meaningful classes and not have to inherit from exception classes just make an object ‘throwable’ ?

How about if I do like this (which is pretty much have I do it currently):

SomeObject obj = null; 
try
{ 
   obj = new SomeObject(); 
}catch {}

is performance slower if obj is defineed inside or outside the try clause as t looks they are both generating identical local to the method (not the try scope) storage variable – you get .locals init ([0] class ClassLibrary.Class obj in the IL either way so maybe its not slower if the variable is never initted ? In other words if I have SomeObject obj = null; defined anywhere inside a method code but I never initialize it to an actual instance of SomeObject, does it matter (performance wise) if my varuable definition is inside the try/catch scope or global to the entire function scope(not inside try/catch) ?

  • 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-18T08:11:38+00:00Added an answer on May 18, 2026 at 8:11 am

    (EDIT: Sorry, I’ve just seen that you suggested this right at the end of your post. Basically it’s the right thing to do!)

    No, you can’t throw anything that doesn’t derive from Exception. But it would be the wrong thing to do anyway – just declare the variable earlier:

    SomeObject obj = null;
    try
    { 
        obj = new SomeObject(); 
        // Other stuff
    }
    catch (IOException e) // Or whatever
    {
        // Now you can refer to obj
    }
    

    Just be aware that obj may be null, if an exception was thrown before the variable was assigned its new value (for example if the SomeObject constructor threw an exception).

    No, you won’t suffer any performance problems due to this – at least, if there are any effects, they’ll be insignificant. The code may end up actually performing an extra assignment, but the chances of that being relevant are practically non-existent.

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

Sidebar

Related Questions

Is there some command line or AppleScript that I can write/run to make the
I'm looking for some kind of text-parser for ASP.NET that can make HTML from
I'd like to make some custom MenuHeaders in WPF so I can have (for
I always believe they did, but seeing some answers here make me doubt... Can
I've seen some horrific code written in Perl, but I can't make head nor
Can some one specify the windows API, one need to use in order to
Can some one post an example of using syslog outputter for log4r, I am
Can some one explain to me the difference between categories and inheritance in Objective
How to pass parameters to [WebMethod] in Asp.Net(C#) ? Can some one please explain
How to read data from Bar Code Scanner in .net windows application? Can some

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.