我正在编写一个小型库,我需要在其中访问几种不同类型的文件。虽然每种文件格式的访问方法本身都不同,但它们似乎有很多共同点,我在类层次结构中放置了一个接口,我在其中编写了一个应该连接到数据源的方法。
但是,由于数据源可能受到密码和/或用户权限的保护,因此有时需要身份验证才能检索数据。我的问题是:
在需要身份验证时抛出异常是个好主意吗?
由于我想尽可能少地公开实现,我只想告诉用户发生了什么。但是身份验证可能需要很多不同的东西(用户名、密码等),所以我可以将它们打包成一个异常并将其丢弃吗?或者,也许有更好的方法而不求助于异常,因为“需要身份验证”实际上并不是异常通常用来处理的异常行为。
需要身份验证时抛出什么异常?
现在假设我决定使用异常来处理这个问题。我应该抛出哪个异常?AuthenticationExceptionJava API 附带的几个s 似乎不符合此要求,因为它们似乎都非常特定于案例,例如,用于命名服务。我不确定这是不是SecurityException要走的路,但如果这是不正确的,我仍然真的不想抛出我自己的异常,因为这会妨碍其他人理解我的代码以及 API 背后发生的事情。
感谢您提供任何意见!这有点冗长或过于冗长,因此非常欢迎任何可以改善问题的编辑。
这是我第一次使用异常处理,所以要温柔。我有一个简单的 blob 类,它接受一个 ID,该 ID 必须在 30 到 50 之间,否则会引发异常。
public class Blob {
int id;
public Blob() {
}
public Blob(int id) throws Exception {
this.id = id;
if (id < 30 || id > 50)
throw new Exception ("id = " +id+ ", must be between 30 and 50 inclusive");
}
}
Run Code Online (Sandbox Code Playgroud)
它应该提示用户输入一个 id,如果它不在 30 到 50 之间,则抛出异常,并且应该继续直到用户输入有效的输入,然后只显示 id 号。
public class BlobCreator {
public static void main(String[] args) {
int id;
Scanner scan = new Scanner(System.in);
System.out.println("Enter …Run Code Online (Sandbox Code Playgroud) 当使用 GNU GDB 调试器调试 C++ 程序时,我可以使用 GDB 命令跳过下一行代码:
next
Run Code Online (Sandbox Code Playgroud)
但是,当在下一行中抛出异常时,例如
throw SomeException();
Run Code Online (Sandbox Code Playgroud)
然后 GDB 继续运行直到下一个断点,而不是在catch块的第一行内停止。
这是 GDB 中的错误,还是我只是使用了错误的命令?
我在MinGW32 / Windows 上使用 GDB 版本 7.7。
好吧,我有一个带有 switch 语句的方法,但我省略了其余的情况,因为它们并不重要。在我的主要方法中,调用运算符方法并在 while 循环中传递参数“选择”,直到他们选择“Q”。
当用户输入负数时,它应该抛出异常,打印一条消息,并忽略他们的输入,然后循环回到开头。当抛出此异常时,它会终止程序。任何帮助将非常感激。谢谢!
public static void operator(String selection) throws IllegalArgumentException{
Scanner input = new Scanner(System.in);
double price;
switch(selection){
case "A":
System.out.println("Enter the price");
if(input.nextDouble()<0){
throw new IllegalArgumentException("Price cannot be a negative value");
}
else{
price = input.nextDouble();
}
break;
case"Q":
System.exit(0);
}
}
Run Code Online (Sandbox Code Playgroud) 这是正确抛出普通 Win32 错误的方法,自动检索错误描述,而且效果非常好:
if (!SomeWinFunc()) {
throw std::system_error(GetLastError(),
std::system_category(),
"SomeWinFunc crashed badly");
}
Run Code Online (Sandbox Code Playgroud)
但是,我不确定如何处理 COM 错误,这些错误的检查方式如下:
HRESULT hr = comObj->SomeFunc();
if (FAILED(hr)) {
throw std::system_error(hr, // <-- is it correct here?
std::system_category(),
"SomeFunc crashed right now");
}
Run Code Online (Sandbox Code Playgroud)
传递HRESULTto是否正确system_error,或者还有其他方法可以从 COM 函数抛出异常?
我有下面的例子。(我的实际项目是一个多线程项目,我为所有项目设置了终止处理程序。)我这里有几个问题。
我的终止处理程序没有做任何花哨的事情。它只是说发生了错误并退出。我读到添加处理程序是一个很好的做法。为什么会这样?在这种情况下我真的需要吗?
如果我没有处理程序,我会得到抛出的异常类型。terminate called after throwing an instance of 'char const*'但是当我使用处理程序时,我无法获取它。即使我使用 current_exception,我也无法获取异常的类型。(这里显然是 char* 但在我的情况下它可能是任何东西,所以我无法正确捕获。即使我使用 catch{...},消息和类型也会丢失)。无论如何都可以得到消息。如果不是消息,至少我可以获得抛出的异常的类型?
// set_terminate example
#include <iostream>
#include <exception>
#include <cstdlib>
using namespace std;
void myterminate () {
cerr << "terminate handler called\n";
abort(); // forces abnormal termination
}
int main (void) {
//set_terminate (myterminate);
throw "TEST"; // unhandled exception: calls terminate handler
return 0;
Run Code Online (Sandbox Code Playgroud) 我写了一个检查各种条件的代码.
如果它满足条件它会做它应该做的,否则我希望它抛出异常.
那有什么特殊的语法吗?否则,由于前置条件,编译器希望我返回任何我不想要的数组.
这是我的代码的一部分:
public static int [] code(int[]arr){
if ((arr!=null)&&(chack4and5(arr))&&(arr[arr.length-1]!=4)&&(TwoFours(arr))){
int k=0;
for(int i = 0; i<=arr.length-1; i++){
if (arr[i] == 4){
int place= pos(arr,k);
arr[place]=arr[i+1];
arr[i+1]=5;
k=k+3;
}
}
return arr;
}
else {
System.out.println("Please enter a legal array which matches the pre- conditions");
}
}
Run Code Online (Sandbox Code Playgroud)
}
两个问题1)当抛出一个Object /变量时会发生什么?比如说,
int foo() {
FILE *fp = ....;
int dummy = 10;
int *dummy_ptr = new int[10];
throw 1;
}
int main() {
try {
foo();
} catch (int &i) {
std::cout<<"ERROR, the value is "<<i<<std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,这里会发生什么?创建一个新变量然后传递???
如果我使用指针或没有引用的变量该怎么办
喜欢catch(int*i)//或catch(int i)
此外,在范围内声明或启动的所有变量/资源是否已被释放/关闭?
2)同样在重新抛出的情况下,如果我计划用引用重新抛出,则第二个catch获取一个新变量,如果我在没有引用(即)值的情况下重新抛出,那么在中间抛出中完成的更改不会受到影响....
int goo() {
throw 2;
}
int foo() {
try{
goo();
} catch(int &i) { // (or) catch(int i) // i is not changing in the next line.
i = 2;
throw;
}
}
int …Run Code Online (Sandbox Code Playgroud) 假设我正在测试下面的React组件jest --coverage:
class MyComponent extends React.Component {
constructor(props) {
super(props)
if (props.invalid) {
throw new Error('invalid')
}
}
}
Run Code Online (Sandbox Code Playgroud)
覆盖率报告将说明该线路throw new Error('invalid')未被发现.既然.not.toThrow()似乎没有涵盖任何东西,我用酶制作以下测试:
const wrapper = shallow(
<MyComponent invalid />
)
it('should throw', () => {
function fn() {
if (wrapper.instance().props.invalid) {
throw new Error('invalid')
}
}
expect(fn).toThrow()
})
Run Code Online (Sandbox Code Playgroud)
这条线被覆盖了!然而,测试本身失败了encountered a declaration exception- 意味着原始组件抛出了错误(应该如此)?
我用toThrow()错了吗?