我已经用Java编写了一段时间了.但有时候,我不明白何时应该抛出异常,何时应该捕获异常.我正在开发一个有很多方法的项目.层次结构是这样的 -
Method A will call Method B and Method B will call some Method C and Method C will call Method D and Method E.
Run Code Online (Sandbox Code Playgroud)
所以我目前正在做的是 - 我在所有方法中抛出异常并在方法A中捕获它然后记录为错误.
但我不确定这是否是正确的方法呢?或者我应该开始捕获所有方法中的异常.所以这就是为什么这种混乱开始于我 - 我应该何时捕获异常与何时应该抛出异常.我知道这是一个愚蠢的问题,但不知怎的,我正在努力理解这个主要概念.
有人能给我一个详细的例子,When to catch the Exception vs When to throw the Exceptions以便我的概念得到澄清吗?在我的情况下,我应该继续抛出异常然后在主调用方法A中捕获它吗?
我可以在php中声明一个抛出异常的函数吗?例如:
public function read($b, $off, $len) throws IOException
Run Code Online (Sandbox Code Playgroud) 考虑:
try {
// Some code here
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw e;
}
Run Code Online (Sandbox Code Playgroud)
throw e和之间有什么区别throw new Exception(e)?
try {
// Some code here
} catch (IOException e) {
throw new IOException(e);
} catch (Exception e) {
throw new Exception(e);
}
Run Code Online (Sandbox Code Playgroud) 如何抛出带有选项或状态代码的错误然后捕获它们?
从这里的语法来看,我们似乎可以通过附加信息来解决错误:
new Error(message, options)
Run Code Online (Sandbox Code Playgroud)
那么,我们可以像下面这样抛出吗?
throw new Error('New error message', { statusCode: 404 })
Run Code Online (Sandbox Code Playgroud)
那么,我们怎样才能抓住它呢statusCode?
try {
//...
} catch (e) {
console.log(e.statusCode) // not working off course!
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
尚不支持选项。
重新抛出错误即可:
try {
const found = ...
// Throw a 404 error if the page is not found.
if (found === undefined) {
throw new Error('Page not found')
}
} catch (error) {
// Re-throw the error with a status code.
error.statusCode = 404 …Run Code Online (Sandbox Code Playgroud) 我用"throw;"重新抛出异常,但堆栈跟踪不正确:
static void Main(string[] args) {
try {
try {
throw new Exception("Test"); //Line 12
}
catch (Exception ex) {
throw; //Line 15
}
}
catch (Exception ex) {
System.Diagnostics.Debug.Write(ex.ToString());
}
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)
正确的堆栈跟踪应该是:
Run Code Online (Sandbox Code Playgroud)System.Exception: Test at ConsoleApplication1.Program.Main(String[] args) in Program.cs:Line 12
但我得到:
Run Code Online (Sandbox Code Playgroud)System.Exception: Test at ConsoleApplication1.Program.Main(String[] args) in Program.cs:Line 15
但第15行是"抛出"的位置.我用.NET 3.5进行了测试.
为什么以下代码行没有创建编译器警告?
void Main()
{
throw new Exception();
throw new Exception();
}
Run Code Online (Sandbox Code Playgroud)
在我看来,编译器应通知您无法达到第二次抛出异常.
我一直在学习Angular 4,一切顺利,直到我尝试在服务中实现catch处理.我正在尝试使用"rxjs"catch并抛出,但我的控制台中有一个未定义的函数错误.
import { Injectable } from '@angular/core';
import { Http } from "@angular/http";
import { Observable } from 'rxjs/observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import { AppError } from "../app/common/app.error";
import { NotFoundError } from "../app/common/not-found-error";
import { BadInput } from "../app/common/bad-input";
@Injectable()
export class PostService {
private url = "https://jsonplaceholder.typicode.com/posts";
constructor(private http: Http) { }
deletepost(post){
// return this.http.delete(this.url + '/' + post.id)
// Hard-coded id to test 404
return this.http.delete(this.url + '/' + 93498)
.catch((error: Response) => { …Run Code Online (Sandbox Code Playgroud) 我试图弄清楚方法签名中的Throws和Java中的Throw Statements之间的区别.方法签名中的引发如下:
public void aMethod() throws IOException{
FileReader f = new FileReader("notExist.txt");
}
Run Code Online (Sandbox Code Playgroud)
抛出语句如下:
public void bMethod() {
throw new IOException();
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,throwsin方法签名是一种通知,该方法可能会抛出这样的异常.throw语句是在相应的情况下实际抛出创建的对象.从这个意义上讲,如果方法中存在throw语句,则应始终显示方法签名中的throws.
但是,以下代码似乎没有这样做.代码来自库.我的问题是它为什么会发生?我理解错误的概念吗?
这段代码是java.util.linkedList的副本.@author Josh Bloch
/**
* Returns the first element in this list.
*
* @return the first element in this list
* @throws NoSuchElementException if this list is empty
*/
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw …Run Code Online (Sandbox Code Playgroud) 如果在我的代码中我有以下代码段:
try {
doSomething();
} catch (...) {
doSomethingElse();
throw;
}
Run Code Online (Sandbox Code Playgroud)
抛出会重新抛出默认省略号处理程序捕获的特定异常吗?
这是代码:
public Response getABC(Request request) throws Exception {
Response res = new Response();
try {
if (request.someProperty == 1) {
// business logic
} else {
throw new Exception("xxxx");
}
} catch (Exception e) {
res.setMessage(e.getMessage); // I think this is weird
}
return res;
}
Run Code Online (Sandbox Code Playgroud)
这个程序运行正常.我认为它应该重新设计,但如何?
throw ×10
exception ×5
java ×4
c# ×2
throws ×2
try-catch ×2
angular ×1
c++ ×1
ellipsis ×1
if-statement ×1
javascript ×1
observable ×1
php ×1
rethrow ×1
rxjs ×1
stack-trace ×1