3.8.自定义异常

定制化异常处理:创建与使用Java异常类的步骤

一.自定义异常操作步骤:

1.定义异常类:类名要见名知意。起名->异常的名字+Exception

2.写继承关系:a.运行时异常,继承RuntimeException;

b.编译时异常,继承Exception

3.写空参构造

4.写带参构造

自定义异常的目的(意义):就是为了让控制台的报错信息更加的见名知意。

以上两个构造方法(应译为Constructor)可用快捷键alt+fn+ins可快速生成

步骤"alt+fn+ins->构造方法->选择要加入的方法即可"

二.举例:

比如之后调用setName方法,若异常,则执行if里的语句。

TypeError: Cannot read property &#39;reading_light_on&#39; of undefined at success (app.js? [sm]:27) at Object.success (WASubContext.js?t=wechat&s=1752416301351&v=3.8.11:1) at Object.success (WASubContext.js?t=wechat&s=1752416301351&v=3.8.11:1) at Function.I.forEach.v.<computed> (WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1) at :15910/appservice/<api request success callback function> at WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 at Nu (WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1) at Function.<anonymous> (WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1) at :15910/appservice/<NetworkRequest15success callback function> at WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1(env: Windows,mp,1.06.2301160; lib: 3.8.11) errorReport @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 thirdErrorReport @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 (anonymous) @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 p @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 (anonymous) @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 Nu @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 (anonymous) @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 p @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 (anonymous) @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 Bm @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 (anonymous) @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 emit @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 u @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 tb @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 (anonymous) @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 (anonymous) @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 (anonymous) @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 (anonymous) @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 _emit @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 emit @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 c @ VM23 asdebug.js:10 (anonymous) @ VM23 asdebug.js:10 a.emit @ VM23 asdebug.js:1 callback @ VM23 asdebug.js:10 (anonymous) @ VM23 asdebug.js:10 setTimeout (async) h @ VM23 asdebug.js:10 n @ VM23 asdebug.js:10 O.onreadystatechange @ VM23 asdebug.js:10 XMLHttpRequest.send (async) v @ VM23 asdebug.js:10 createRequestTask @ VM23 asdebug.js:10 invoke @ VM23 asdebug.js:10 r @ VM23 asdebug.js:10 invoke @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 pn @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 (anonymous) @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 Up @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 Y_ @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 Q_ @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 u @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 J_ @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 yM @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 createRequestTask @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 execute @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 (anonymous) @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 tct @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 I @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 p @ WAServiceMainContext.js?t=wechat&s=1752416301351&v=3.8.11:1 p @ WASubContext.js?t=wechat&s=1752416301351&v=3.8.11:1 value @ apihook.js?t=wechat&s=1752416301351&v=0cffbba9918afb1342c18e2fdfb25ef5:1 onLaunch @ app.js? [sm]:20 (anonymous) @ WASubContext.js?t=wechat&s=1752416301351&v=3.8.11:1 Rt @ WASubContext.js?t=wechat&s=1752416301351&v=3.8.11:1 (anonymous) @ WASubContext.js?t=wechat&s=1752416301351&v=3.8.11:1 (anonymous) @ WASubContext.js?t=wechat&s=1752416301351&v=3.8.11:1 (anonymous) @ app.js? [sm]:3 (anonymous) @ WASubContext.js?t=wechat&s=1752416301351&v=3.8.11:1 runWith @ WASubContext.js?t=wechat&s=1752416301351&v=3.8.11:1 q @ WASubContext.js?t=wechat&s=1752416301351&v=3.8.11:1 (anonymous) @ VM513:7 doWhenAllScriptLoaded @ VM494:1342 (anonymous) @ VM494:15 loadBabelModules @ assubloader.js:1 async function (async) loadBabelModules @ assubloader.js:1 window.loadBabelMod @ VM449:10 (anonymous) @ toConsumableArray.js:2 Show 32 more frames WAServiceMainC
07-14
<think>我们正在处理一个微信小程序开发中常见的TypeError:`Cannot read property &#39;reading_light_on&#39; of undefined`。这个错误通常发生在试图访问一个未定义对象的属性时。根据提供的引用和类似错误的分析,我们可以推断出几种可能的原因和解决方案。 ### 错误原因分析 1. **对象未初始化**:可能你试图访问一个尚未初始化的对象的属性。例如,在组件的`data`中未定义`reading_light_on`属性,或者在访问时该对象为`undefined`。 2. **作用域问题(this指向)**:在回调函数中,`this`可能不再指向当前页面或组件实例,导致访问`this.data`或`this.setData`时出错(引用[3]提到了类似问题)。 3. **异步数据处理**:可能在数据还未获取到(例如从服务器请求的数据)时就尝试访问其属性。 4. **路径引用问题**:如果`reading_light_on`是某个组件的属性,可能该组件未正确加载或初始化(类似引用[4]中组件加载问题)。 ### 解决方案 #### 1. 确保对象已初始化 在页面的`data`中初始化`reading_light_on`属性。例如: ```javascript Page({ data: { reading_light_on: false // 初始值 } }) ``` #### 2. 检查this的指向 在回调函数(如网络请求、定时器等)中使用`this`时,确保它指向页面实例。可以使用箭头函数或提前保存`this`。例如: ```javascript Page({ onLoad: function() { // 方法1:使用箭头函数 setTimeout(() => { this.setData({ reading_light_on: true }) }, 1000) // 方法2:保存this const that = this wx.request({ success: function(res) { that.setData({ reading_light_on: res.data.reading_light_on }) } }) } }) ``` #### 3. 确保数据存在再访问 在访问嵌套属性时,使用安全访问(可选链操作符`?.`)或条件判断。例如: ```javascript // 使用可选链操作符(需要基础库2.9.0以上) if (this.data.object?.reading_light_on) { // 操作 } // 或条件判断 if (this.data.object && this.data.object.reading_light_on) { // 操作 } ``` #### 4. 检查组件或模块引用 如果`reading_light_on`是来自自定义组件,请确保组件已正确引入并初始化。检查组件路径是否正确(类似引用[4]的问题)。 ### 其他建议 - **调试工具**:使用微信开发者工具中的断点调试功能,查看出错时变量的值。 - **日志输出**:在访问`reading_light_on`之前,使用`console.log`输出对象,确认其是否已定义。 ### 示例代码修正 假设你有一个控制阅读灯的功能,代码可能如下: ```javascript Page({ data: { readingLight: { // 初始化一个对象 reading_light_on: false } }, turnOnLight: function() { // 正确访问:通过data中初始化的对象 this.setData({ &#39;readingLight.reading_light_on&#39;: true }) }, // 在回调中使用箭头函数确保this指向 onReady: function() { wx.request({ url: &#39;https://example.com/light-status&#39;, success: (res) => { // 使用可选链避免访问未定义属性 const lightStatus = res.data?.reading_light_on if (lightStatus !== undefined) { this.setData({ &#39;readingLight.reading_light_on&#39;: lightStatus }) } } }) } }) ``` ### 相关问题 1. 如何避免微信小程序中常见的`this.setData`未定义错误? 2. 微信小程序中如何处理异步数据加载时的渲染问题? 3. 如何调试微信小程序中的TypeError异常? 希望这些解决方案能帮助你解决问题。如果问题仍然存在,请检查代码中访问`reading_light_on`的地方,确保对象已定义。</think>### 微信小程序 `TypeError: Cannot read property &#39;reading_light_on&#39; of undefined` 解决方案 这个错误表示你正在尝试访问一个未定义对象(`undefined`)的 `reading_light_on` 属性。结合引用文档和常见案例,以下是完整排查步骤和解决方案: --- #### 核心原因分析 1. **对象未初始化**:访问的父级对象未定义(如 `this.data` 中缺少对象定义) 2. **作用域问题**:回调函数中 `this` 指向错误(常见于异步请求) 3. **数据加载时机问题**:在数据初始化前尝试访问属性 4. **拼写错误**:属性名或变量名拼写不一致 --- ### 一、基础排查与修复方案 #### 1. 确保对象初始化(最常见) 在 `data` 中明确定义包含 `reading_light_on` 的对象: ```javascript Page({ data: { // 必须初始化对象结构 lightConfig: { reading_light_on: false // 初始值 } } }) ``` #### 2. 修复 `this` 指向问题(关键步骤) 使用 **箭头函数** 或 **临时变量** 锁定作用域: ```javascript // 方案1:箭头函数(推荐) wx.request({ success: (res) => { this.setData({ &#39;lightConfig.reading_light_on&#39;: res.data.status }) } }) // 方案2:保存 this 引用 const that = this wx.getSystemInfo({ success: function(res) { that.setData({ &#39;lightConfig.reading_light_on&#39;: res.platform === &#39;android&#39; }) } }) ``` #### 3. 添加安全访问校验 使用可选链操作符(需基础库 2.9.0+): ```javascript // 安全访问方式 if (this.data.lightConfig?.reading_light_on) { // 执行操作 } ``` #### 4. 检查异步数据加载顺序 确保数据加载完成后再渲染: ```wxml <view wx:if="{{lightConfig}}"> 灯光状态:{{lightConfig.reading_light_on ? &#39;开启&#39; : &#39;关闭&#39;}} </view> ``` --- ### 二、进阶排查步骤 #### 1. 检查拼写一致性 全局搜索 `reading_light_on` 确认: - WXML 模板中的绑定名 - JS 中的 `setData` 键名 - 初始化 `data` 中的字段名 是否完全一致(注意大小写) #### 2. 组件生命周期问题 在 `onLoad` 或 `onReady` 中初始化数据: ```javascript onReady() { this.setData({ lightConfig: this.data.lightConfig || { reading_light_on: false } }) } ``` #### 3. 排查第三方组件 如果使用了自定义组件: ```javascript // 检查组件传值 <light-component config="{{lightConfig}}" // 确保传入对象已初始化 /> ``` --- ### 三、调试技巧 1. **添加日志定位**: ```javascript console.log(&#39;当前对象:&#39;, this.data.lightConfig) // 检查是否为undefined ``` 2. **启用开发者工具异常断点**: - 在开发者工具中点击 Sources → 开启 Pause on exceptions 3. **真机调试**: - 通过真机远程调试查看完整堆栈 --- ### 典型修复案例 ```javascript // 修复前(错误示例) Page({ data: {}, // 未初始化对象 onLoad() { wx.request({ success(res) { // this 指向错误! this.setData({ &#39;lightConfig.reading_light_on&#39;: res.data.status }) } }) } }) // 修复后 Page({ data: { lightConfig: { reading_light_on: false } // 初始化对象 }, onLoad() { const that = this wx.request({ success: (res) => { // 使用箭头函数 that.setData({ &#39;lightConfig.reading_light_on&#39;: res.data.status }) } }) } }) ``` > **经验提示**:微信小程序中 80% 的此类错误可通过初始化数据 + 修复 `this` 指向解决[^1][^3] --- ### 相关问题 1. 如何避免微信小程序中常见的 `undefined` 引用错误? 2. 微信小程序异步请求中如何正确管理 `this` 作用域? 3. 小程序开发中如何安全访问深层嵌套的对象属性? 4. 真机调试时如何捕获未在模拟器出现的 `TypeError`? [^1]: uniapp - 微信小程序报错MiniProgramError Cannot read property ‘dataset‘ of undefined TypeError: ... [^3]: 微信小程序报错 TypeError: Cannot read property ‘setData‘ of undefined
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值