为什么C#中的这段代码不会抛出错误

时间:2015-05-17 19:17:17

标签: c#

理想情况下,变量returnString是局部变量并且未初始化将无法使用,并且会出现编译时错误:

  

使用未分配的局部变量'returnString'

事实上,这就是我在另一个背景下得到的。它仍然有效。为什么?

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;

 namespace ConsoleApplication1
 {
     class a {
         public static string testMethod(){
             string returnString;
             try
             {
                 int a = 100;
                 int b = 0;
                 b = a / b;
                 returnString= Convert.ToString(a - b);
                 // return returnString;
             }
             catch (Exception ex)
             {
                  return  returnString = Convert.ToString(ex);
                  //throw ex;
             }
             return returnString;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // a A = new a();
            Console.WriteLine(a.testMethod());
            Console.ReadKey();
        }
    }
}

4 个答案:

答案 0 :(得分:2)

在这种情况下,反编译代码(使用发布模式)通常会有所帮助。这是dotPeek生成的反编译源:

2015-05-17T18:23:35.994186+00:00 app[web.1]:  vendor/bundle/ruby/2.0.0/gems/railties-4.1.8/lib/rails/application.rb:144:in    `call' 2015-05-17T18:23:35.994189+00:00 app[web.1]:      vendor/bundle/ruby/2.0.0/gems/rack-1.5.3/lib/rack/handler/webrick.rb:60:in `service' 2015-05-17T18:23:35.994188+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.5.3/lib/rack/content_length.rb:14:in `call' 2015-05-17T18:23:35.994191+00:00 app[web.1]:   vendor/ruby-   2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service' 2015-05-  17T18:23:35.994193+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/
webrick/server.rb:295:in `block in start_thread'
2015-05-17T18:23:35.994192+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
2015-05-17T18:23:35.994194+00:00 app[web.1]:
2015-05-17T18:23:35.994195+00:00 app[web.1]:
2015-05-17T18:25:56.190647+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=unstarv.herokuapp.com request_id=e7b2dd76-e168-4266-9937-7043f922 1f fwd="158.222.193.118" dyno=web.1 connect=1ms service=3ms status=304 bytes=133 2015-05-17T18:26:04.445519+00:00 heroku[router]: at=info method=GET path="/users/auth/facebook" host=unstarv.herokuapp.com request_id=b8db408e-b809-4c7a-a0d1-a43c84cd4b0c fwd="158.222.193.118" dyno=web.1 connect=2ms service=7ms status=302 bytes=2216 2015-05 17T18:26:04.568894+00:00 app[web.1]: Started GET "/users/auth/facebook" for        158.222.193.118 at 2015-05-17 18:26:04 +0000 2015-05 17T18:26:04.777296+00:00    app[web.1]: Started GET "/users/auth/facebook" for 158.222.193.118 at 2015-05-17 18:26:04 +0000 2015-05-17T18:26:04.570840+00:00 app[web.1]: (facebook) Request phase initiated.  2015-05-17T18:26:04.778818+00:00 app[web.1]: (facebook) Request phase initiated.

2015-05-17T18:26:04.653315+00:00 heroku[router]: at=info method=GET path="/users/auth/facebook" host=unstarv.herokuapp.com request_id=ae31cf01-31f1-49bc-ac48-e75ef6adc128 fwd="158.222.193.118" dyno=web.1 connect=0ms service=6ms status=302 bytes=2216 2015-05-17T18:26:05.068973+00:00 app[web.1]: Started GET"/users/auth/facebook/callback code=AQAcbQnJ0PzHQ3V4VXBUiFGM9XLOU69rdc1DyFcgNMOl0g5tVJgjJEoyrzjZFf552LkokSwjx-yelsbi8JIfwIWMrZp-mg0e8APWaOY1PhxWMe9---Nc_dsqpf7xVVScoRW RxHeByV553pCt7a8DWxErbBphKE1ZXT7jEK0oTmDDzocYycNwVTD8UAbrPl6PuLMYo6DAYsnkYtJbo21fnaskFyrHbKHrIbtGFKVpMGPPwD8k1aqx720LK4V2DOG_wLoeCQoQv2yBsHEBxr_tMFgE6UR1pCjPvUglQPqlxX7zU0bh4o_AfppcOpQ&state=ed94cae9b2066f6b0823907259f6df3f166a18cd5bc39674" for 158.222.193.118 at 2015-05-17 18:26:05 +0000 2015     0517T18:26:05.070832+00:00 app[web.1]: (facebook) Callback phaseinitiated
2015-05-17T18:26:05.344081+00:00 app[web.1]: Processing by OmniauthCallbacksController#facebook as HTML 2015-05-17T18:26:05.344091+00:00 app[web.1]:   Parameters: {"code"=>"AQAcbQnJ0PzHQ3V4VXBUiFGM9XLOU69rdc1DyFcgNMOl0g5tVJgjJEoyrzjZFf552LkokSwjx-yelsbi8JIfwIWMrZp-mg0e8APWaOY1PhxWMe9--BKzcCHRfqu2h1P5WwMwSjUpN70t_PuHpafIuQh2bYvkjEbNNi1-Nc_dsqpf7xVVScoRW-RxHeByV553pCt7a8DWxErbBphKE1ZXT7jEK0oTmDDzocYycNwVTD8UAbrPl6PuLMYo6DAYsnkYtJbo21fnaskFyrHbKHrIbtGFKVpMGPPwD8k1aqx720LK4V2DOG_wLoeCQoQv2yBsHEBxr_tMFgE6UR1pCjPvUglQPqlxX7zU0bh4o_AfppcOpQ","state"=>"ed94cae9b2066f6b0823907259f6df3f166a18cd5bc39674"} 2015-05-17T18:26:05.355010+00:00 app[web.1]: Completed 500 Internal Server Errorin 11ms 2015-05-17T18:26:05.230943+00:00 heroku[router]: at=info method=GET path="/users/auth/facebook/callback?code=AQAcbQnJ0PzHQ3V4VXBUiFGM9XLOU69rdc1DyFcgNMOl0g5tVJgjJEoyrzjZFf552LkokSwjx-yelsbi8JIfwIWMrZp-mg0e8APWaOY1PhxWMe9--BKzcCHRfqu2h1P5WwMwSjUpN70t_PuHpafIuQh2bYvkjEbNNi1-Nc_dsqpf7xVVScoRW-RxHeByV553pCt7a8DWxErbBphKE1ZXT7jEK0oTmDDzocYycNwVTD8UAbrPl6PuLMYo6DAYsnkYtJbo21fnaskFyrHbKHrIbtGFKVpMGPPwD8k1aqx720LK4V2DOG_wLoeCQoQv2yBsHEBxr_tMFgE6UR1pCjPvUglQPqlxX7zU0bh4o_AfppcOpQ&state=ed94cae9b2066f6b0823907259f6df3f166a18cd5bc39674" host=unstarv.herokuapp.com request_id=47ffd209-44db-44b0-b772-02a987639e25 fwd="158.222.193.118" dyno=web.1 connect=1ms service=292ms status=500 bytes=1754 2015-05-17T18:26:05.357087+00:00 app[web.1]: 2015-05-17T18:26:05.357089+00:00 app[web.1]: NoMethodError (undefined method `current_sign_in_at' for<User:0x007f134796b0e8>):20150517T18:26:05.357091+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activemodel-4.1.8/lib/active_model/attribute_methods.rb:435:in `method_missing'2015-05-17T18:26:05.357092+00:00 app[web.1]:  vendor/bundle/ruby/2.0.0/gems/activerecord4.1.8/lib/active_record/attribute_methods.rb:213:in `method_missing' 2015-05-17T18:26:05.357093+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/devise-3.4.1/lib/devise/models/trackable.rb:19:in `update_tracked_fields'2015-05-17T18:26:05.357095+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/devise-3.4.1/lib/devise/models/trackable.rb:32:in `update_tracked_fields!'2015-05-17T18:26:05.357096+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/devise-3.4.1/lib/devise/hooks/trackable.rb:7:in `block in <top (required)>'2015-05-17T18:26:05.357098+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/warden-1.2.3/lib/warden/hooks.rb:14:in `call'
2015-05-17T18:26:05.357099+00:00  app[web.1]:vendor/bundle/ruby/2.0.0/gems/warden-1.2.3/lib/warden/hooks.rb:14:in `block in _run_callbacks'2015-05-17T18:26:05.357100+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/warden-1.2.3/lib/warden/hooks.rb:9:in `each'2015 05-17T18:26:05.357101+00:00 app[web.1]:vendor/bundle/ruby/2.0.0/gems/warden- 1.2.3/lib/warden/hooks.rb:9:in `_run_callbacks'2015-05n17T18:26:05.357102+00:00 app[web.1]:vendor/bundle/ruby/2.0.0/gems/warden1.2.3/lib/warden/manager.rb:53:in `_run_callbacks' 
2015-05-17T18:26:05.357104+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/warden-1.2.3/lib/warden/proxy.rb:179:in `set_user'2015-05-17T18:26:05.357105+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/devise3.4.1/lib/devise/controllers/sign_in_out.rb:43:in `sign_in'2015-05-17T18:26:05.357106+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/devise3.4.1/lib/devise/controllers/helpers.rb:235:in `sign_in_and_redirect'2015-05-17T18:26:05.357107+00:00 app[web.1]:   (eval):6:in `facebook' 2015-05-17T18:26:05.357109+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_controller/metal/implicit_render.rb:4:in `send_action'2015-05-17T18:26:05.357110+00:00 app[web.1]:vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/abstract_controller/base.rb:189:in `process_action'2015-05-17T18:26:05.357113+00:00 app[web.1]:  vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_controller/metal/rendering.rb:10:in `process_action' 2015-05-17T18:26:05.357114+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack/4.1.8/lib/abstract_controller/callbacks.rb:20:in `block in process_action'2015-05-17T18:26:05.357115+00:00 app[web.1]:vendor/bundle/ruby/2.0.0/gems/activesupport 4.1.8/lib/active_support/callbacks.rb:113:in `call'2015-05-17T18:26:05.357116+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport4.1.8/lib/active_support/callbacks.rb:113:in `call'2015-05-17T18:26:05.357118+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/activesupport4.1.8/lib/active_support/callbacks.rb:229:in `block in halting'2015-05-17T18:26:05.357119+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport4.1.8/lib/active_support/callbacks.rb:229:in `call'2015-05-17T18:26:05.357121+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport4.1.8/lib/active_support/callbacks.rb:229:in `block in halting' 2015-05-17T18:26:05.357122+00:00 app[web.1]:  vendor/bundle/ruby/2.0.0/gems/activesupport/4.1.8/lib/active_support/callbacks.rb:166:in `call' 2015-05-17T18:26:05.357123+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/callbacks.rb:166:in `block in halting'
2015-05-17T18:26:05.357124+00:00 app[web.1]:  vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/callbacks.rb:166:in `call' \
  2015-05-17T18:26:05.357125+00:00 app[web.1]:vendor/bundle/ruby/2.0.0/gems/activesupport4.1.8/lib/active_support/callbacks.rb:166:in `block in halting'
 2015-05-17T18:26:05.357127+00:00 app[web.1]:  vendor/bundle/ruby/2.0.0/gems/activesupport4.1.8/lib/active_support/callbacks.rb:166:in `call' 2015-05-17T18:26:05.357128+00:00 app[web.1]:vendor/bundle/ruby/2.0.0/gems/activesupport4.1.8/lib/active_support/callbacks.rb:166:in `block in halting'2015-05-17T18:26:05.357129+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport4.1.8/lib/active_support/callbacks.rb:166:in `call'
2015-05-17T18:26:05.357130+00:00 app[web.1]:  vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/callbacks.rb:166:in `block in halting'
2015-05-17T18:26:05.357131+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/callbacks.rb:166:in `call 2015-05-17T18:26:05.357132+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport4.1.8/lib/active_support/callbacks.rb:166:in `block in halting'
2015-05-17T18:26:05.357134+00:00 app[web.1]:  vendor/bundle/ruby/2.0.0/gems/activesupport4.1.8/lib/active_support/callbacks.rb:86:in `call'
2015-05-17T18:26:05.357135+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport4.1.8/lib/active_support/callbacks.rb:86:in `run_callbacks'
2015-05-17T18:26:05.357136+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack/lib/abstract_controller/callbacks.rb:19:in `process_action'
 2015-05-17T18:26:05.357137+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_controller/metal/rescue.rb:29:in `process_action'
 2015-05-17T18:26:05.357138+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'                                     2015-05-17T18:26:05.357139+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/notifications.rb:159:in `block in instrument
'2015-05-17T18:26:05.357144+00:00 app[web.1]:        vendor/bundle/ruby/2.0.0/gems/activesupport4.1.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
2015-05-17T18:26:05.357145+00:00 app[web.1]:vendor/bundle/ruby/2.0.0/gems/activesupport4.1.8/lib/active_support/notifications.rb:159:in `instrument'
2015-05-17T18:26:05.357146+00:00 app[web.1]: vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
2015-05-17T18:26:05.357148+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'2015-05-17T18:26:05.357149+00:00 app[web.1]:  vendor/bundle/ruby/2.0.0/gems/activerecord4.1.8/lib/active_record/railties/controller_runtime.rb:18:in `process_action'2015-05-17T18:26:05.357150+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.1.8/lib/abstract_controller/base.rb:136:in `process'2015-05-17T18:26:05.357151+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionview4.1.8/lib/action_view/rendering.rb:30:in `process'2015-05-17T18:26:05.357152+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_controller/metal.rb:196:in `dispatch'2015-05-17T18:26:05.357153+00:00 app[web.1]:     vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'2015-05-17T18:26:05.357154+00:00 app[web.1]:     vendor/bundle/ruby/2.0.0/gems/actionpack-4.1.8/lib/action_controller/metal.rb:232:in `block in action'
2015-05-17T18:26:05.357156+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/routing/route_set.rb:82:in `call'
2015-05-17T18:26:05.357157+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
2015-05-17T18:26:05.357158+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_dispatch/routing/route_set.rb:50:in `call' 2015-05-17T18:26:05.357159+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_dispatch/routing/mapper.rb:45:in `call'2015-05-17T18:26:05.357161+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_dispatch/journey/router.rb:73:in `block in call'2015-05-17T18:26:05.357162+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_dispatch/journey/router.rb:59:in `each'
2015-05-17T18:26:05.357163+00:00 app[web.1]:      vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_dispatch/journey/router.rb:59:in `call' 2015-05-17T18:26:05.357164+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_dispatch/routing/route_set.rb:678:in `call'
2015-05-17T18:26:05.357165+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/omniauth-1.2.2/lib/omniauth/strategy.rb:404:in `call_app!'
 2015-05-17T18:26:05.357166+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/omniauth-1.2.2/lib/omniauth/strategy.rb:362:in `callback_phase'
 2015-05-17T18:26:05.357168+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/omniauth-oauth2-1.3.0/lib/omniauth/strategies/oauth2.rb:75:in `callback_phase'
2015-05-17T18:26:05.357169+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/omniauthfacebook2.0.1/lib/omniauth/strategies/facebook.rb:73:in `block in callback_phase'
2015-05-17T18:26:05.357170+00:00 app[web.1]:    vendor/bundle/ruby/2.0.0/gems/omniauthfacebook2.0.1/lib/omniauth/strategies/facebook.rb:136:in `with_authorization_code!'
2015-05-17T18:26:05.357171+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/omniauth-facebook2.0.1/lib/omniauth/strategies/facebook.rb:72:in `callback_phase'
2015-05-17T18:26:05.357173+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/omniauth-1.2.2/lib/omniauth/strategy.rb:227:in `callback_call' 2015-05-17T18:26:05.357174+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/omniauth-1.2.2/lib/omniauth/strategy.rb:184:in `call!'
2015-05-17T18:26:05.357175+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/omniauth-1.2.2/lib/omniauth/strategy.rb:164:in `call'
2015-05-17T18:26:05.357176+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/omniauth-1.2.2/lib/omniauth/builder.rb:59:in `call'
2015-05-17T18:26:05.357177+00:00 app[web.1]:  vendor/bundle/ruby/2.0.0/gems/warden-1.2.3/lib/warden/manager.rb:35:in `block in call'
2015-05-17T18:26:05.357178+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/warden-1.2.3/lib/warden/manager.rb:34:in `catch'
2015-05-17T18:26:05.357180+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/warden-1.2.3/lib/warden/manager.rb:34:in `call'
2015-05-17T18:26:05.357181+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.5.3/lib/rack/etag.rb:23:in `call'
2015-05-17T18:26:05.357182+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.5.3/lib/rack/conditionalget.rb:25:in `call'
2015-05-17T18:26:05.357183+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.5.3/lib/rack/head.rb:11:in `call'
2015-05-17T18:26:05.357184+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_dispatch/middleware/params_parser.rb:27:in `call'
2015-05-17T18:26:05.357185+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_dispatch/middleware/flash.rb:254:in `call'
 2015-05-17T18:26:05.357187+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.5.3/lib/rack/session/abstract/id.rb:225:in `context'
2015-05-17T18:26:05.357188+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.5.3/lib/rack/session/abstract/id.rb:220:in `call'
2015-05-17T18:26:05.357189+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_dispatch/middleware/cookies.rb:560:in `call'
2015-05-17T18:26:05.357190+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activerecord4.1.8/lib/active_record/query_cache.rb:36:in `call'
2015-05-17T18:26:05.357194+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activerecord4.1.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
2015-05-17T18:26:05.357196+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
2015-05-17T18:26:05.357197+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/callbacks.rb:82:in `run_callbacks'
2015-05-17T18:26:05.357198+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
2015-05-17T18:26:05.357199+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
2015-05-17T18:26:05.357200+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
2015-05-17T18:26:05.357201+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack-4.1.8/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
2015-05-17T18:26:05.357202+00:00 app[web.1]:  vendor/bundle/ruby/2.0.0/gems/railties-4.1.8/lib/rails/rack/logger.rb:38:in `call_app'
2015-05-17T18:26:05.357204+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.1.8/lib/rails/rack/logger.rb:20:in `block in call'
2015-05-17T18:26:05.357205+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport4.1.8/lib/active_support/tagged_logging.rb:68:in `block in tagged'
2015-05-17T18:26:05.357206+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport4.1.8/lib/active_support/tagged_logging.rb:26:in `tagged'
2015-05-17T18:26:05.357207+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport4.1.8/lib/active_support/tagged_logging.rb:68:in `tagged'
2015-05-17T18:26:05.357208+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.1.8/lib/rails/rack/logger.rb:20:in `call'
 2015-05-17T18:26:05.357209+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_dispatch/middleware/request_id.rb:21:in `call'
2015-05-17T18:26:05.357211+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.5.3/lib/rack/methodoverride.rb:21:in `call'
 2015-05-17T18:26:05.357212+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.5.3/lib/rack/runtime.rb:17:in `call'
2015-05-17T18:26:05.357213+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/activesupport4.1.8/lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
 2015-05-17T18:26:05.357214+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/actionpack4.1.8/lib/action_dispatch/middleware/static.rb:84:in `call'
2015-05-17T18:26:05.357215+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.5.3/lib/rack/sendfile.rb:112:in `call'
2015-05-17T18:26:05.357216+00:00 app
 in `call'
2015-05-17T18:26:05.357217+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/railties-4.1.8/lib/rails/application.rb:144:in `call'
 2015-05-17T18:26:05.357219+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.5.3/lib/rack/lock.rb:17:in `call'
 2015-05-17T18:26:05.357220+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.5.3/lib/rack/content_length.rb:14:in `call'
 2015-05-17T18:26:05.357221+00:00 app[web.1]:   vendor/bundle/ruby/2.0.0/gems/rack-1.5.3/lib/rack/handler/webrick.rb:60:in `service'
 2015-05-17T18:26:05.357222+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
 2015-05-17T18:26:05.357223+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
 2015-05-17T18:26:05.357224+00:00 app[web.1]:   vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
 2015-05-17T18:26:05.357225+00:00 app[web.1]:
 2015-05-17T18:26:05.357226+00:00 app[web.1]:
 2015-05-17T18:26:05.361104+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=unstarv.herokuapp.com request_id=a24209c3-7d23-487c-b67b-b729060cb473 fwd="158.222.193.118" dyno=web.1 connect=0ms service=6ms status=304 bytes=133

正如您所看到的,对于编译器,没有未初始化的变量。那为什么要抱怨?

答案 1 :(得分:1)

因为编译器知道它将在稍后分配。

这不能编译,因为在返回之前可能没有分配变量(例如,如果a == 2):

    private string method(int a)
    {
        string result;

        if (a == 1)
        {
            result = "2334234";           
        }
        return result;
    }

答案 2 :(得分:1)

向我看,好像每个通过该函数的路径都返回一个初始化变量。要么您成功地将除法转换为零,要么您将有一个例外,您可以为变量分配异常的值。您总是会有一个正确分配的值,编译器会看到它。

答案 3 :(得分:0)

如果符合以下条件,您的代码会设置returnString

  1. returnString块中设置try/catch变量之前,代码不会抛出异常。

  2. 如果try块中的代码引发异常,则会分配returnString

  3. 结论:没有returnString变量可以取消分配的情况。