错误的异常被抛出(Wrong exception being thrown)
我将字符串传递给此方法。 字符串是“44”。 然后我将它解析为一个int并执行if语句以查看它是否在1到12之间。然后转到我所拥有的else语句 - 抛出新的IllegalArgumentException(“非法月份数量:”+ inSetString);
由于某种原因,这不会被抛出,它会下降到下一个异常并抛出一个 - 抛出新的IllegalArgumentException(“无效的月份名称字符串:”+ inSetString);
为什么它抛出第二个异常语句而不是第一个异常语句? 它会转到else语句。 我通过使用--System.out.println(“除了”+ inSetString)测试了它;
这是我的代码:
public void setMonthName(String inSetString) { int i = 0; monthNumber = -1; try { System.out.println("first" + inSetString); i = Integer.parseInt(inSetString); System.out.println("next" + inSetString); if (i >= 1 && i <= 12) { monthNumber = i; } else { System.out.println("except" + inSetString); throw new IllegalArgumentException("Illegal month number here: " + inSetString); } } catch (IllegalArgumentException e) { } // if it gets to here we know it's a string for (int index = 0; index < monthNames.length; index++) { if (monthNames[index].equalsIgnoreCase(inSetString)) { monthNumber = index; } } if (monthNumber == -1) { throw new IllegalArgumentException("Invalid month name string: " + inSetString); } }
I'm passing in a string to this method. The string is "44". I then parse it into an int and do an if statement to see if it's between 1 and 12. It then goes to the else statement where I have - throw new IllegalArgumentException("Illegal month number here: " + inSetString);
For some reason, that doesn't get thrown and it goes down to the next exception and that one is thrown - throw new IllegalArgumentException("Invalid month name string: " + inSetString);
Why does it throw the second exception statement but not the first? It DOES go to the else statement. I've tested that by using - System.out.println("except" + inSetString);
Here's my code:
public void setMonthName(String inSetString) { int i = 0; monthNumber = -1; try { System.out.println("first" + inSetString); i = Integer.parseInt(inSetString); System.out.println("next" + inSetString); if (i >= 1 && i <= 12) { monthNumber = i; } else { System.out.println("except" + inSetString); throw new IllegalArgumentException("Illegal month number here: " + inSetString); } } catch (IllegalArgumentException e) { } // if it gets to here we know it's a string for (int index = 0; index < monthNames.length; index++) { if (monthNames[index].equalsIgnoreCase(inSetString)) { monthNumber = index; } } if (monthNumber == -1) { throw new IllegalArgumentException("Invalid month name string: " + inSetString); } }
原文:https://stackoverflow.com/questions/9355476
最满意答案
您应该可以使用扩展Ninject.Extensions.ContextPreservation 。 特别是扩展方法
IContext.ContextPreservingGet(...)
:var connector = context.ContextPreservingGet<IInternalConnector>();
但是,我个人认为创建特定的设置类型是更好的选择 - 因为它是更简单的想法。
You should be able to use the extension Ninject.Extensions.ContextPreservation. Specifically the extension method
IContext.ContextPreservingGet(...)
:var connector = context.ContextPreservingGet<IInternalConnector>();
However, personally I think that creating specific settings types is the better choice - because it's the simpler idea.
相关问答
更多-
如何用ninject在HttpModule中注入依赖关系?(How to inject dependencies in an HttpModule with ninject?)[2022-01-07]
Phil Haack在博客中介绍了如何实现这一点,从而可以使用构造函数注入,从而避免使您的HttpModule直接依赖于Ninject。 在标准的NinjectHttpApplication ,执行以下操作: 步骤1 使用Nuget查找并将HttpModuleMagic包添加到您的Web项目中。 第2步 编写你的HttpModule来使用构造器注入: public class MyHttpModule : IHttpModule { public MyHttpModule(ISomeService ... -
您提供的链接来自2.2已过时。 只需将该页面的ob切换到主分支即可 The link you provided is from 2.2 which is outdated. Just switch ob that page to the master branch
-
Ninject Providers:解决依赖关系的正确方法是什么?(Ninject Providers: what is the correct way to resolve dependencies)[2022-05-26]
您应该可以使用扩展Ninject.Extensions.ContextPreservation 。 特别是扩展方法IContext.ContextPreservingGet(...) : var connector = context.ContextPreservingGet(); 但是,我个人认为创建特定的设置类型是更好的选择 - 因为它是更简单的想法。 You should be able to use the extension Ninject.Extensi ... -
指定类型参数不是运行时的东西,它是静态编译的。 必须在编译时知道该类型。 在您的方案中,它可能是未知的,或在运行时计算。 通过反射,可以构造一个方法调用,您可以在其中指定类型参数,但是您不太可能想要这样做。 此外,大多数容器应该有一个看起来像这样的重载: Type ninjaType = typeof(Ninja); var ninja = (Ninja)ninject.Get(ninjaType); 最后,大多数容器应该提供在容器配置中指定的方法,在某些条件下应该提供哪种类型。 我知道Ninject有 ...
-
初始化对象后,Ninject可以解决抽象依赖关系吗?(Can Ninject resolve abstract dependencies after the object is initialised?)[2022-12-01]
我认为您正在寻找的是以下情况: public class InviteFriend { [Required] public string EmailAddress { get; set; } // More information } public interface ICommand { void Execute(); } public class InviteFriendCommand : ICommand { public InviteFriend(Inv ... -
Ninject,Providers和Activator.CreateInstance(Ninject, Providers and Activator.CreateInstance)[2023-12-10]
第一个问题不错! 你想使用Bind(Type)重载来允许你在Load()代码的上下文中没有静态可用的东西的注册 - 在你的提供者中做你正在做的事情(即解析Type )面前。 这将允许Ninject执行对象实例化(不需要默认的.ctor) IIRC 我最近的两个或三个答案也涉及这个发现/加载的东西,并有与您的案例相关的例子。 (并且当你去除东西时,你不需要使用[Inject]属性) Not bad for a first question! You want to use the Bind(Type) ove ... -
你不应该直接要求一个ILogger (虽然错误信息可能会更好)但是你应该解决一个ILoggerFactory ,你可以要求一个ILogger _kernel = new StandardKernel(); ILoggerFactory loggerFactory = _kernel.Get
(); ILogger logger = loggerFactory.GetCurrentClassLogger(); 另请参阅文档自己请求记录器部分。 You should not di ... -
我找到了解决方案。 对我而言,这只是一个愚蠢的错误。 我的依赖项绑定未正确配置。 如果只有我得到一个更有帮助的例外...... I found the solution. It was simply a bone-headed mistake on my part. My dependency bindings were not configured correctly. If only I had been given a more helpful exception...
-
我正在查看Ninject的提供程序和激活上下文 ,看起来Providers是一个处理Autofac只能处理lambda的场景的接口。 在Ninject的例子中,他们有: Bind
().ToProvider(new SwordProvider()); abstract class SimpleProvider { // Simple implementations of the junk that you don't care about... public object ... -
我不太确定你的问题是什么,但你想要做的是使用你的MainFactory类来构建所有其他类,还使用Ninject来构建“第二级”依赖项。 对我而言,这似乎是一种非常奇怪的方法。 您是否需要使用MainFactory来编写顶级对象? 这将表明一个设计问题,其中一个“上帝对象”被用来做很多事情。 每当一个类需要一个新的依赖项时,你还必须改变你的Ninject绑定和你的MainFactory方法,这种方式首先打破了使用依赖注入框架的重点。 您是否可以使用适当的Ninject绑定替换MainFactory ,如果在运 ...