- Learning Apex Programming
- Matt Kaufman Michael Wicherski
- 237字
- 2021-07-23 20:54:22
Embracing an exception
The Exception
class includes various methods that help you better understand why an exception was thrown. You can even find out the type of exception, what line of Apex code caused the exception, and even get the entire stack trace. In addition to the built-in exception types, Apex includes the ability to create your own exception types. This allows you to catch a system exception, evaluate it, and throw your own custom exception. Although it involves slightly more work, a custom exception type can be the difference between frantically debugging code in the middle of the night and sleeping like a baby. Imagine writing code to integrate with an outside system. There are times where that process will fail (like when the other system is under maintenance). If you proactively throw your own exception, you'll immediately know why it failed. The following code will show you how to define a custom exception type:
//Define our custom Exception type Public class myCustomException extends Exception{} //Try calling out to our outside system try { httpRequest req = new httpRequest(); //You have to whitelist the endpoint in Setup|Remote Site Settings req.setEndpoint('http://www.madeUpUrl.net'); req.setMethod('GET'); //Set the timeout to 1 millisecond to force an exception req.setTimeout(1); httpResponse res = new http().send(req); } catch ( Exception e){ if (e.getTypeName() == 'System.CalloutException' && e.getMessage() == 'Read timed out'){ throw new myCustomException('Go Back to Sleep', e); } else { throw e; } }
- Java逍遙游記
- UI設計基礎培訓教程
- JSP網(wǎng)絡編程(學習筆記)
- Learning Docker
- Machine Learning with R Cookbook(Second Edition)
- WordPress Plugin Development Cookbook(Second Edition)
- 用Flutter極速構(gòu)建原生應用
- PHP+MySQL+Dreamweaver動態(tài)網(wǎng)站開發(fā)實例教程
- Python數(shù)據(jù)挖掘與機器學習實戰(zhàn)
- TypeScript項目開發(fā)實戰(zhàn)
- 微信小程序項目開發(fā)實戰(zhàn)
- Java網(wǎng)絡編程核心技術詳解(視頻微課版)
- “笨辦法”學C語言
- 大話Java:程序設計從入門到精通
- Extending Unity with Editor Scripting