异常处理开关:try{}catch(...){}

本文深入探讨了在VS2010中使用/EH开关来配置异常处理模型的方法,包括同步和异步异常处理的区别及应用实例。
try{}catch(...){};这条语句在我们平时程序中会经常用到,当异常发生时能自动捕获。而对于程序异常时,我们可以通过参数
/EH{s|a}[c][-]

来设置要捕获对象。

Arguments
a

The exception-handling model that catches asynchronous (structured) and synchronous (C++) exceptions.

s

The exception-handling model that catches C++ exceptions only and tells the compiler to assume that extern C functions do throw an exception.

c

If used with s (/EHsc), catches C++ exceptions only and tells the compiler to assume that extern C functions never throw a C++ exception. /EHca is equivalent to /EHa.

Remarks

Use /EHs to specify the synchronous exception handling model (C++ exception handling without structured exception handling exceptions). If you use /EHs, then your catch clause will not catch asynchronous exceptions. Also, all objects in scope when the asynchronous exception is generated will not be destroyed even if the asynchronous exception is handled. Under /EHs, catch(...) will only catch C++ exceptions. Access violations and System.Exception exceptions will not be caught.

Use /EHa to specify the asynchronous exception handling model (C++ exception handling with structured exception handling exceptions). /EHa may result in a less performant image because the compiler will not optimize a try block as aggressively, even if the compiler does not see a throw.

Use /EHa if you want to catch an exception raised with something other than a throw. The following sample will generate an exception:

// compiler_options_EHA.cpp
// compile with: /EHa
#include <iostream>
#include <excpt.h>
using namespace std;

void fail() {   // generates SE and attempts to catch it using catch(...)
   try {
      int i = 0, j = 1;
      j /= i;   // This will throw a SE (divide by zero).
      printf("%d", j); 
   }
   catch(...) {   // catch block will only be executed under /EHa
      cout<<"Caught an exception in catch(...)."<<endl;
   }
}

int main() {
   __try {
      fail(); 
   }

   // __except will only catch an exception here
   __except(EXCEPTION_EXECUTE_HANDLER) {   
   // if the exception was not caught by the catch(...) inside fail()
      cout << "An exception was caught in __except." << endl;
   }
}

该开关位置:

VS2010中如下图

12-16 15:53:04.523480 20264 22747 W System.err: android.content.pm.PackageManager$NameNotFoundException: com.google.android.wearable.app 12-16 15:53:04.523613 20264 22747 W System.err: at android.app.ApplicationPackageManager.getPackageInfoAsUser(ApplicationPackageManager.java:304) 12-16 15:53:04.523636 20264 22747 W System.err: at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:264) 12-16 15:53:04.523651 20264 22747 W System.err: at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:258) 12-16 15:53:04.523663 20264 22747 W System.err: at o2.f.n(BugleNotifications.java:16) 12-16 15:53:04.523675 20264 22747 W System.err: at o2.s.m(MessageNotificationState.java:1) 12-16 15:53:04.523693 20264 22747 W System.err: at o2.s.o(MessageNotificationState.java:63) 12-16 15:53:04.523710 20264 22747 W System.err: at o2.f.h(BugleNotifications.java:4) 12-16 15:53:04.523722 20264 22747 W System.err: at o2.f.v(BugleNotifications.java:191) 12-16 15:53:04.523736 20264 22747 W System.err: at com.android.messaging.datamodel.action.ReceiveSmsMessageAction.executeAction(ReceiveSmsMessageAction.java:1105) 12-16 15:53:04.523750 20264 22747 W System.err: at com.android.messaging.datamodel.action.CoroutineAction.a(ThreadAction.kt:128) 12-16 15:53:04.523766 20264 22747 W System.err: at com.android.messaging.datamodel.action.CoroutineAction$start$2.invokeSuspend(ThreadAction.kt:29) 12-16 15:53:04.523798 20264 22747 W System.err: at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:8) 12-16 15:53:04.523812 20264 22747 W System.err: at kotlinx.coroutines.q0.run(DispatchedTask.kt:112) 12-16 15:53:04.523828 20264 22747 W System.err: at kotlinx.coroutines.internal.m$a.run(LimitedDispatcher.kt:4) 12-16 15:53:04.523841 20264 22747 W System.err: at re.i.run(Tasks.kt:3) 12-16 15:53:04.523854 20264 22747 W System.err: at kotlinx.coroutines.scheduling.CoroutineScheduler$a.run(CoroutineScheduler.kt:102) 这段日志是什么意思
最新发布
12-19
09-11 00:40:47.673 10169 7138 7138 W System.err: at android.app.ApplicationPackageManager.getPackageInfoAsUser(ApplicationPackageManager.java:283) 09-11 00:40:47.673 10169 7138 7138 W System.err: at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:243) 09-11 00:40:47.673 10169 7138 7138 W System.err: at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:237) 09-11 00:40:47.673 10169 7138 7138 W System.err: at ti0.b(SogouSource:13) 09-11 00:40:47.673 10169 7138 7138 W System.err: at ti0.h(SogouSource:115) 09-11 00:40:47.673 10169 7138 7138 W System.err: at com.sohu.inputmethod.sogou.u.H(SogouSource:184) 09-11 00:40:47.673 10169 7138 7138 W System.err: at com.sohu.inputmethod.sogou.MainImeServiceDel.initSoftKeyboardCacheWhenInitializeInterface(SogouSource:20) 09-11 00:40:47.673 10169 7138 7138 W System.err: at com.sohu.inputmethod.sogou.MainImeServiceDel.doWhenInitialInterfaceIOFinishFirstTime(SogouSource:30) 09-11 00:40:47.673 10169 7138 7138 W System.err: at com.sohu.inputmethod.sogou.t.d(SogouSource:339) 09-11 00:40:47.673 10169 7138 7138 W System.err: at com.sohu.inputmethod.sogou.AbstractImeServiceDelegate$InnerHandler.handleMessage(SogouSource:5) 09-11 00:40:47.673 10169 7138 7138 W System.err: at android.os.Handler.dispatchMessage(Handler.java:110) 09-11 00:40:47.673 10169 7138 7138 W System.err: at android.os.Looper.loopOnce(Looper.java:265) 09-11 00:40:47.673 10169 7138 7138 W System.err: at android.os.Looper.loop(Looper.java:358) 09-11 00:40:47.673 10169 7138 7138 W System.err: at android.app.ActivityThread.main(ActivityThread.java:10080) 09-11 00:40:47.673 10169 7138 7138 W System.err: at java.lang.reflect.Method.invoke(Native Method) 09-11 00:40:47.673 10169 7138 7138 W System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:616) 09-11 00:40:47.673 10169 7138 7138 W System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1115) 09-11 00:40:47.674 1000 2709 2904 W ActivityManager: Unknown package:com.xiaomi.aiservice 09-11 00:40:47.674 10169 7138 7138 W System.err: android.content.pm.PackageManager$NameNotFoundException: com.miui.core 09-11 00:40:47.674 10169 7138 7138 W System.err: at android.app.ApplicationPackageManager.getPackageInfoAsUser(ApplicationPackageManager.java:283) 09-11 00:40:47.674 10169 7138 7138 W System.err: at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:243) 09-11 00:40:47.674 1000 2709 2903 V ActivityManager: Attempted to start a foreground service (com.android.mms/com.xiaomi.mms.transaction.MxActivateService) with a broken notification (no icon: Notification(channel=Channel_Foreground_Service shortcut=null contentView=null vibrate=null sound=null defaults=0 flags=FOREGROUND_SERVICE color=0x00000000 groupKey=mms_foreground_service_group vis=PRIVATE)) 09-11 00:40:47.674 10169 7138 7138 W System.err: at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:237) 09-11 00:40:47.674 10169 7138 7138 W System.err: at ti0.c(SogouSource:13) 09-11 00:40:47.674 10169 7138 7138 W System.err: at ti0.h(SogouSource:126) 09-11 00:40:47.674 10169 7138 7138 W System.err: at com.sohu.inputmethod.sogou.u.H(SogouSource:184) 09-11 00:40:47.674 10169 7138 7138 W System.err: at com.sohu.inputmethod.sogou.MainImeServiceDel.initSoftKeyboardCacheWhenInitializeInterface(SogouSource:20) 09-11 00:40:47.674 10169 7138 7138 W System.err: at com.sohu.inputmethod.sogou.MainImeServiceDel.doWhenInitialInterfaceIOFinishFirstTime(SogouSource:30) 09-11 00:40:47.674 10169 7138 7138 W System.err: at com.sohu.inputmethod.sogou.t.d(SogouSource:339) 09-11 00:40:47.674 10169 7138 7138 W System.err: at com.sohu.inputmethod.sogou.AbstractImeServiceDelegate$InnerHandler.handleMessage(SogouSource:5) 09-11 00:40:47.674 10169 7138 7138 W System.err: at android.os.Handler.dispatchMessage(Handler.java:110) 09-11 00:40:47.674 10169 7138 7138 W System.err: at android.os.Looper.loopOnce(Looper.java:265) 09-11 00:40:47.674 10169 7138 7138 W System.err: at android.os.Looper.loop(Looper.java:358) 09-11 00:40:47.674 10169 7138 7138 W System.err: at android.app.ActivityThread.main(ActivityThread.java:10080) 09-11 00:40:47.674 10169 7138 7138 W System.err: at java.lang.reflect.Method.invoke(Native Method) 09-11 00:40:47.674 10169 7138 7138 W System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:616) 09-11 00:40:47.674 10169 7138 7138 W System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1115)
09-18
08-05 15:41:03.347 1192 1192 D BluetoothHidHost: Proxy object disconnected 08-05 15:40:56.240 1232 1232 I chatty : uid=1000(system) ctc.android.smart.terminal.settings identical 2 lines 08-05 15:40:56.242 1232 1232 W ViewRootImpl[MoreActivity]: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=490.63416, y[0]=411.8772, toolType[0]=TOOL_TYPE_MOUSE, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=1100162, downTime=1098858, deviceId=5, source=0x2002 } 08-05 15:41:03.347 353 1309 I ActivityManager: Process com.android.bluetooth (pid 597) has died: psvc PER 08-05 15:41:03.347 353 353 D BluetoothManagerService: BluetoothServiceConnection, disconnected: com.android.bluetooth.btservice.AdapterService 08-05 15:41:03.348 353 405 W libprocessgroup: kill(-597, 9) failed: No such process 08-05 15:41:03.348 353 1309 W ActivityManager: Scheduling restart of crashed service com.android.bluetooth/.a2dp.A2dpService in 1000ms 08-05 15:41:03.348 353 415 E BluetoothManagerService: MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED(1) 08-05 15:41:03.348 353 1309 W ActivityManager: Scheduling restart of crashed service com.android.bluetooth/.btservice.AdapterService in 1000ms 08-05 15:41:03.348 353 1309 W ActivityManager: Scheduling restart of crashed service com.android.bluetooth/.avrcp.AvrcpTargetService in 11000ms 08-05 15:41:03.349 353 1309 W ActivityManager: Scheduling restart of crashed service com.android.bluetooth/.opp.BluetoothOppService in 11000ms 08-05 15:41:03.349 353 1309 W ActivityManager: Scheduling restart of crashed service com.android.bluetooth/.gatt.GattService in 11000ms 08-05 15:41:03.349 353 415 D BluetoothManagerService: MESSAGE_BLUETOOTH_SERVICE_DISCONNECTED hasDevice:true mEnable:true 08-05 15:41:03.349 353 415 D BluetoothManagerService: Broadcasting onBluetoothServiceDown() to 5 receivers. 08-05 15:41:03.349 353 1309 W ActivityManager: Scheduling restart of crashed service com.android.bluetooth/.hid.HidDeviceService in 21000ms 08-05 15:41:03.349 353 1309 W ActivityManager: Scheduling restart of crashed service com.android.bluetooth/.hid.HidHostService in 20999ms 08-05 15:41:03.350 353 415 E BluetoothManagerService: Unable to call onBluetoothServiceDown() on callback #0 08-05 15:41:03.350 353 415 E BluetoothManagerService: android.os.DeadObjectException 08-05 15:41:03.350 353 415 E BluetoothManagerService: at android.os.BinderProxy.transactNative(Native Method) 08-05 15:41:03.350 353 415 E BluetoothManagerService: at android.os.BinderProxy.transact(Binder.java:1129) 08-05 15:41:03.350 353 415 E BluetoothManagerService: at android.bluetooth.IBluetoothManagerCallback$Stub$Proxy.onBluetoothServiceDown(IBluetoothManagerCallback.java:109) 08-05 15:41:03.350 353 415 E BluetoothManagerService: at com.android.server.BluetoothManagerService.sendBluetoothServiceDownCallback(BluetoothManagerService.java:1356) 08-05 15:41:03.350 353 415 E BluetoothManagerService: at com.android.server.BluetoothManagerService.access$4700(BluetoothManagerService.java:83) 08-05 15:41:03.350 353 415 E BluetoothManagerService: at com.android.server.BluetoothManagerService$BluetoothHandler.handleMessage(BluetoothManagerService.java:1799) 08-05 15:41:03.350 353 415 E BluetoothManagerService: at android.os.Handler.dispatchMessage(Handler.java:106) 08-05 15:41:03.350 353 415 E BluetoothManagerService: at android.os.Looper.loop(Looper.java:193) 08-05 15:41:03.350 353 415 E BluetoothManagerService: at android.os.HandlerThread.run(HandlerThread.java:65) 08-05 15:41:03.350 353 415 E BluetoothManagerService: at com.android.server.ServiceThread.run(ServiceThread.java:44) 08-05 15:41:03.350 353 415 D BluetoothAdapter: onBluetoothServiceDown: android.bluetooth.IBluetooth$Stub$Proxy@d412eff 08-05 15:41:03.350 1232 1232 D BluetoothHidHost: Proxy object disconnected 08-05 15:41:03.350 3205 3239 D BluetoothAdapter: onBluetoothServiceDown: android.bluetooth.IBluetooth$Stub$Proxy@f8f674f 08-05 15:41:03.351 1232 1232 D BluetoothHidHost: Unbinding service... 08-05 15:41:03.351 353 415 D BluetoothManagerService: Sending BLE State Change: ON > TURNING_OFF
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值