질문,답변

글 수 1,629

[re] 예외처리는 어떤방식으로하나요..?

조회 수 23027 추천 수 0 2008.02.15 22:37:35
허창원 *.68.46.59
저는 잘 모르지만, 도움말의 색인에 Exception Handling 이라고 쳐보니 아래와같은 설명이 나오네요.

VISA 사용시에 해당되는 것 같군요...

----------------------------------------------------------------
Exception Handling
By using the VISA event VI_EVENT_EXCEPTION, you can have one point in your code that traps all errors and handles them appropriately. This means that after you install and enable your VISA exception handler, you do not have to check the return status from each operation, which makes the code easier to read and maintain. How an application handles error codes is specific to both the device and the application. For one application, an error could mean different things from different devices, and might even be ignored under certain circumstances; for another, any error could always be fatal.

For an application that needs to treat all errors as fatal, one possible use for this event type would be to print out a debug message and then exit the application. Because the method of installing the handler and then enabling the event has already been covered, the following code segment shows only the handler itself:

ViStatus _VI_FUNCH myEventHandler (ViSession vi, ViEventType etype, ViEvent eventContext, ViAddr uHandle)
{

ViChar rsrcName[256], operName[256];
ViStatus stat;
ViSession rm;

if (etype == VI_EVENT_EXCEPTION) {

viGetAttribute(vi,VI_ATTR_RSRC_NAME,rsrcName);
viGetAttribute(eventContext,VI_ATTR_OPER_NAME,operName);
viGetAttribute(eventContext,VI_ATTR_STATUS,&stat);
printf(
"Session 0x%08lX to resource %s caused error 0x%08lX in operation %s.\n",
vi,rsrcName,stat,operName);

/* Use this code only if you will not return control to VISA */
viGetAttribute(vi,VI_ATTR_RM_SESSION,&rm);
viClose(eventContext);
viClose(vi);
viClose(rm);
exit(-1); /* exit the application immediately */

}
/* code for other event types */
return VI_SUCCESS;

}


If you wanted just to print a message, you would leave out the code that closes the objects and exits. Notice that in this code segment, the event object is closed inside of the callback, even though we just recommended in the previous section that you not do this! The reason that we do it here is that the code will never return control to VISA—calling exit() will return control to the operation system instead. This is the only case where you should ever invoke viClose() within a callback.

Another (more advanced) use of this event type is for throwing C++ exceptions. Because VISA exception event handlers are invoked in the context of the same thread in which the error condition occurs, you can safely throw a C++ exception from the VISA handler. Like the example above, you would invoke viClose() on the exception event (but you would probably not close the actual session or its resource manager session). You would also need to include the information about the VISA exception (for example, the status code) in your own exception class (of the type that you throw), since this will not be available once the VISA event is closed.

Throwing C++ exceptions introduces several issues to consider. First, if you have mixed C and C++ code in your application, this could introduce memory leaks in cases where C functions allocate local memory on the heap rather than the stack. Second, if you use asynchronous operations, an exception is thrown only if the error occurs before the operation is posted (for example, if the error generated is VI_ERROR_QUEUE_ERROR). If the error occurs during the operation itself, the status is returned as part of the VI_EVENT_IO_COMPLETION event. This is important because that event may occur in a separate thread, due to the nature of asynchronous I/O. Therefore, you should not use asynchronous operations if you want to throw C++ exceptions from your handler.


맨날초보

2008.04.10 11:38:20
*.210.1.244

감사합니다.^^
List of Articles
번호 제목 글쓴이 날짜 조회 수
공지 이제부터 질문은 LabWindows/CVI 카페를 이용하면 더 좋겠습니다 운영자 2020-11-11 1235
공지 파일(특히 그림파일)을 올리실 때의 유의사항 운영자 2004-07-16 100157
1109 DAQ카드를 통한 Data Acquitiion에 대해서 질문드려요. [2] 정은주 2008-04-23 1766
1108 Open한 파일의 확장자를 아는 방법 [1] CVI초보 2008-04-18 2199
1107 RS-232통신 시 Hex Data Value로 볼 수 있는 방법에 대해 질문 드립니다. file 신아진 2008-04-18 2434
1106 [re] Hex Data Value로 볼 수 있는 방법 - C언어 활용편 file [1] 허창원 2008-04-19 2542
1105 VC6.0에서 gwgraph사용 관련 왈츠군 2008-04-18 2477
1104 serial 통신(RS-232)관련 질문드립니다. 김해성 2008-04-17 1848
1103 [re] serial 통신(RS-232)관련 질문드립니다. [1] 허창원 2008-04-17 4964
1102 특정 위치에서 DOS창이 실행되고 특정 파일을 자동으로 실행시키는 방법 [1] CVI초보 2008-04-17 1891
1101 FATAL RUN-TIME ERROR [2] 김재환 2008-04-14 1987
1100 텝(TAB) 컨트롤이 되지 않아 질문 드립니다. file mun-jejin 2008-04-10 2095
1099 [re] 텝(TAB) 컨트롤이 되지 않아 질문 드립니다. file [2] 박재성 2008-04-11 3148
1098 테이블에 관해 궁금한게 있어서 이렇게 글을 올립니다. [2] 박헌주 2008-03-31 2044
1097 그래프에 관해서 질문드리겠습니다. [2] 박헌주 2008-03-11 1740
1096 실행파일과 외부파일을 동시에 실행하는 방법알려주세요? [2] 한국인 2008-03-03 2007
1095 배포판 작성 시 30일 한정판 만드는 방법좀 알려주세요 [1] 김정욱 2008-03-03 1885
1094 선택한 파일명을 출력하는 방법 공대생 2008-02-15 1939
1093 예외처리는 어떤방식으로하나요..? [1] 맨날초보 2008-02-04 1852
» [re] 예외처리는 어떤방식으로하나요..? [1] 허창원 2008-02-15 23027
1091 이곳의 자료를 사용해도 되나요? [2] super-star 2008-02-01 1599
1090 응용화일생성 관련문의 [1] 민형석 2008-01-28 1577