抛出异常 [英] Exception throw

查看:82
本文介绍了抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



异常对象是否有可能知道它正在抛出?

我想在抛出异常时登录文本文件,不是在创建

异常对象时(因为我可以创建异常对象

永远不会被抛出)。

谢谢,

Nenad

解决方案

您好


是否有可能异常对象要知道它正在抛出?
我想在抛出exeption时登录文本文件,而不是在创建
异常对象时(因为我可以创建异常对象)永远不会抛出。
谢谢,Nenad




我认为没有理由创建Exception对象/>
不会被抛出。但我不想解决我自己的问题。编码

标准。


如果你想记录异常的抛出,那么没有更好的

来记录它比这个例外的最后一个(或第一个)捕获。


使用:


尝试{

/ / YourException dangerous

}

catch(YourException ex){

//用于登录文本文件的地方

抛出;

}


问候


Marcin


< blockquote>目前没有办法让异常对象本身检测到什么时候抛出它,但是你的代码很容易明确地记录它什么时候它会抛出它。例外。你想要完成什么?

" Nenad Dobrilovic" < CH ******* @ hotmail.com>在消息中写道

新闻:呃************** @ TK2MSFTNGP09.phx.gbl ...

异常对象是否有可能意识到它正在抛出?
我想在抛出异常时登录文本文件,而不是在创建
异常对象时(因为我可以创建)
从不抛出异常对象。
谢谢你,
Nenad



大多数人创建自己的应用程序异常而不是使用

泛型System.Exception,所以我假设你也做了。你能做什么
做的是在你的异常类上创建一个Factory方法并让它创建

异常,记录到某处然后抛出异常。以下是

简短示例。

class MyException:Exception {

public MyException(String msg):base(msg){

}

public static void ExceptionFactory(String msg){

MyException除了= new MyException(msg);

//登录

System.Console.Out.WriteLine(msg);

throw(除外);

}

}


这样称呼它。

MyException.ExceptionFactory(" Test Exception");

只是一个想法希望它有帮助。

Leon Lambert


Nenad Dobrilovic写道:


异常对象是否有可能请注意它正在抛出?
我想在抛出exeption时登录文本文件,而不是在创建
异常对象时(因为我可以创建异常对象,
是从未抛出过。
谢谢你,
Nenad



Hi,
Is it possible for exception object to be aware of it''s throwing?
I want to log in the text file when exeption is thrown, not when the
exception object is created (because I can create exception object that
is never thrown).
Thank you,
Nenad

解决方案

Hi

Hi,
Is it possible for exception object to be aware of it''s throwing?
I want to log in the text file when exeption is thrown, not when the
exception object is created (because I can create exception object that
is never thrown).
Thank you,
Nenad



I think that there is no reason to create Exception object if it
will not be thrown. But i don''t want to settle "my own" coding
standards.

If you want to log exception''s throw, so there is no better
place to log it than the last (or first) catch of this exception.

use:

try {
// YourException danger
}
catch(YourException ex) {
// place for logging to text file
throw;
}

Regards

Marcin


There currently is no means for the exception object itself to detect when
it is thrown, but it is easy for your code to explicitly log it when it
throws the exception. What are you trying to accomplish?
"Nenad Dobrilovic" <ch*******@hotmail.com> wrote in message
news:uh**************@TK2MSFTNGP09.phx.gbl...

Hi,
Is it possible for exception object to be aware of it''s throwing?
I want to log in the text file when exeption is thrown, not when the
exception object is created (because I can create exception object that
is never thrown).
Thank you,
Nenad



Most people create their own application exceptions instead of using the
generic System.Exception so i will assume you did also. What you could
do is create a Factory method on you exception class and have it create
the exception, log to somewhere then throw the exception. Following is a
short example.
class MyException : Exception{
public MyException(String msg) : base(msg){
}
public static void ExceptionFactory(String msg){
MyException except = new MyException(msg);
// log it
System.Console.Out.WriteLine(msg);
throw(except);
}
}

call it like this.
MyException.ExceptionFactory("Test Exception");
Just an idea hope it helps.
Leon Lambert

Nenad Dobrilovic wrote:

Hi,
Is it possible for exception object to be aware of it''s throwing?
I want to log in the text file when exeption is thrown, not when the
exception object is created (because I can create exception object that
is never thrown).
Thank you,
Nenad



这篇关于抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆